Page Template (Best for Single Pages)
If you only need to call a different sidebar on a single page and that page is totally unique, and there’s no need to have a plan for children of those pages, then a simple page template should do the trick. Just go to your parent theme, copy page.php
(for classic themes) or page.html
(for block themes) and paste it into the root directory of your child theme. You can also create page templates WYSIWYG style with blocks, but that’s for another time.
Rename page.php
to include the slug of the one page you’re looking to change. If you want example.com/awesome-page
to be different, then rename page.php
to be page-awesome-page.php
. Open your freshly renamed file and search for the get_sidebar()
function that is likely used in your page.php
file and pass the name or ID of the sidebar you want into that function.
Conditional Statement (Best for Groups of Pages)
I had a situation in which I needed to manage a handful of different sidebars that all needed to be displayed on different pages, depending on what kind of page it is. For example, it it’s a page about bicycles, it needs to display the bicycle sidebar. If it’s a child page of the bicycle page, it also needs to display the bicycle sidebar. If it’s a page or child page about underwater basket weaving, it needs to display the sidebar for basket weaving. Else, just output a normal generic sidebar. This little function came in great handy to be placed in the child theme’s page.php
:
<?php
if (is_page([ID OR SLUG]) || $post->post_parent == [postID] ) {
wp_nav_menu(array('menu' => 'menu_name'));
}
?>
It should also be mentioned: the wp_nav_menu()
function, can kinda take just a normal menu name or ID, but if you were like me and needed to stack a few if..elseif..elseif...elseif..else
statements, I ended up seeing the same menu being outputted regardless of the ID or name passed into wp_nav_menu()
. That was resolved when I passed the menu name into an array, like above. The first element of the array must be 'menu'
, and the second element is the name of whatever your menu is.