Hi Paal
I’ve found there is already a good tutorial on adding another sidebar to your theme: https://www.tastyplacement.com/add-sidebar-wordpress
But to go through it quickly, in your functions.php you register your new sidebar.
register_sidebar(array(
'name' => __( 'Sidebar two', 'twentytwelve' ),
'id' => 'sidebarTwo',
'description' => 'Sidebar No. 2',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' ));
In your theme folder create a new file that will contain the code for your new sidebar. It should be named sidebar-whatever.php. In this file you need to call the sidebar you registered, ‘Sidebar two’ in my example. And if you are adding a menu placement in this sidebar, you need to set the menu location using wp_nav_menu. So, my file ‘sidebar-2.php’ contains:
<div id="sidebar2Container" class="widget-area" role="complementary">
<ul>
<?php dynamic_sidebar('Sidebar two');?>
<?php wp_nav_menu( array( 'theme_location' => 'sidebar two' ) );?>
</ul>
</div>
At the moment we have placed where our custom menu will show up, but we haven’t actually made that placement available to choose in the admin area. To do, in your Functions.php file add:
register_nav_menu( 'sidebar two', 'Sidebar 2' );
Now when you create a menu in the admin area, you should see ‘Sidebar 2’ listed under the theme locations where the menu can appear.
Finally, we need to make it so that our sidebar will actually appear. To do this, edit the page template(s) that you want to include the new sidebar. For this example, I’m going to add the new sidebar only to single pages, so I’m just going to edit single.php.
At the bottom of single.php I added the call to my new sidebar. The last 3 lines of my single.php look like this:
<?php get_sidebar(); //get the default sidebar ?>
<?php get_sidebar('2'); //get our sidebar 2 (sidebar-2.php) ?>
<?php get_footer(); ?>
As you can see, my sidebar will be included straight after the primary sidebar. You can, of course, place the call to the sidebar before the content, or wherever you want. Note that I call get_sidebar('2')
as my sidebar file is named sidebar-2.php
. If my sidebar file was named sidebar-my-super-sidebar.php
, then I would need to call get_sidebar('my-super-sidebar')
to have it included.
And that’s it really. For my own purposes that I originally asked this question about, I did something quite different. There are often multiple different ways to get the same thing done. But I believe the above process should probably suit most people wanting to add a second sidebar with a menu location.
Hopefully someone will correct me if any of the above is wrong. (It works for me but there could be some possible conflict or best practice way of doing things that I have missed).
Dave