How to Add a Sidebar to a Wordpress Template

104 23
    • 1). Create the "sidebar.php" file if one does not already exist for your theme. Add to this file any code needed for the sidebar, such as a <div> or <section> containers. Save the file. Then, open your "index.php" file and check it for the following code:

      <?php get_sidebar(); ?>

      If this code does not exit, add it to the code in the file where you want the sidebar to appear. Style with CSS rules as desired. Test that the new sidebar shows correctly by visiting your WordPress blog.

    • 2). Widgetize your sidebar by adding the following conditional statement:

      <?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar()) : ?>

      This code goes in the "sidebar.php" file wherever you want the widgets to appear. Below this conditional statement, add template tags such as <?php wp_get_archives(); ?> that will appear when the user has not yet set his own widgets. After adding that code, type <?php endif; ?> to end the conditional statement.

    • 3). Open or create the "functions.php" file in your code editor of choice. Add the following code to the end or beginning of this file:

      <?php if(function_exists('register_sidebar')) {register_sidebar();} ?>

      This code makes your theme widget-ready.

    • 4). Reload your WordPress website and check that the default template tags you added to the sidebar work. Go to the dashboard for your site and click on "Appearance" and then "Widgets" in the left-hand menu. Check that the theme now has at least one sidebar. Drag and drop widgets into the sidebar from within the dashboard and then visit your WordPress site again. Check that only the widgets you added in the dashboard are now showing on the website.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.