Front-end Web Developer | Tampa, FL

How to add new sidebar in Your WordPress theme?

Posted on: February 23rd, 2010 by admin No Comments
  • Now, when you go to your WordPress admin section and browse to the widgets under the menu item called presentation, you will see two sidebars listed there. You can drag your widget items into any of the sidebars.
  • Now comes the part where we actually build the sidebars. If your theme has only one sidebar, try to locate a file called sidebar.php in your theme folder. In this example, where we are trying to modify the theme for two sidebars, let’s rename sidebar.php to sidebar1.php and make a new blank file called sidebar2.php.
  • Put this code into sidebar2.php and save the file:

    {CODE type:php;}
    <div>
    <ul>
    <?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2) ) : else : ?>
    <?php endif; ?>
    </ul>
    </div>
    {/CODE}

  • So, we have the two sidebars ready but they have not been placed in the index.php file yet. Both these sidebars need to be called from the index.php file in order to include them in your theme. Just open the index.php file from your theme folder and locate the code that calls your sidebar file (sidebar.php earlier). It should look something like:

    {CODE type:php;}<?php include (TEMPLATEPATH . ‘/sidebar.php’); ?>{/CODE}

  • Edit this code and change the words sidebar.php to sidebar1.php.
  • This takes care of the first sidebar. Now take a look at the index.php file carefully and find a suitable place to insert the second sidebar. This might involve modifying your layout or adding new divs. Once you find a suitable place, place the following code there:

    {CODE type:php;}<?php include (TEMPLATEPATH . ‘/sidebar2.php’); ?>{/CODE}

  • Save the index.php file and now preview your theme. You will see all the widgets that you placed in both your sidebars appearing on your website. If you have not placed any widgets yet, you will not see any change. There might be alignment errors but you will have to fix them yourself. You can add more sidebars in a similar way to your WordPress theme. I hope this tutorial helps some of you.
  • I took this article from this blog.

    Comments are closed.