In a follow-up to the last tutorial on how to define extra sidebars in WordPress, I am going to show you how to add a different sidebar to each page on your site.
If you have not read the post from last week then I suggest you read the tutorial first. Let’s get started.
1. Add a new page within the WordPress admin.
Navigate to pages > add new and create a new page, for the purposes of this post we will call this blog. Add page title and your content then save the page using publish.
2. Duplicate your sidebar.php.
First you need to duplicate your sidebar.php file and rename it. The new sidebar file should be named in the following format, sidebar-name.php, so for example if you want a new sidebar for your blog page you can name the file, sidebar-blog.php. Next you need to add the following code to your new sidebar file as shown in last weeks tutorial.
if (is_active_sidebar( 'first-sidebar' ) ) : //Check if Sidebar is active in admin dynamic_sidebar( 'first-sidebar' ); //Add Sidebar to page endif;
You can amend which sidebar to display within the (‘ ‘).
3. Duplicate your page.php file.
Next you need to duplicate your page.php file and rename it. Now we need to tell WordPress to use the new page, which can be done in 2 ways. The first method is to rename your file to match your page as defined in the WordPress admin in the first step in the Pages tab. The way that WordPress knows to use this page, is by renaming the theme template to page-pagename.php. So if you have a page defined in admin as blog, then you would rename your theme file as page-blog.php. WordPress will then look for this file and display your page using the this template file.
The second way, is by assigning a template to a WordPress page by the use of a defined template code. In the top of the your page.php file you should see code similar to the following.
The code may differ dependant on your installed theme but will follow the same format. This is mainly for identification and is used for some internal functions within WordPress. Did you notice the line shown below?
* Template Name: Blog
By using this code, you will be able to select the page within the admin using the page attributes tab on within the Pages panel as shown in the following image.
The benefit of using this method is that you can use this template for many pages whereas the earlier method is restricted to one page with a set name.
4. Add your sidebar to your page template.
Open the earlier template file you duplicated from page.php and look for the following code.
Now within the parenthesis add the name of your sidebar as you defined earlier, so if you named your sidebar file “sidebar-blog.php” then you should add the name ‘blog’ within the parenthesis as shown below.
When you navigate to http://www.yoursite.co.uk/blog You should now see a new page with your new sidebar defined. You can now add widgets to your new sidebar under Appearance > Widgets. You can repeat this for every page that you require an alternate sidebar to be shown.
Hope you enjoyed this tutorial and if you have anything to add please leave a comment below.