Add new sidebar


I’m trying to add a new sidebar. I only want to display this particular sidebar on a category page. Otherwise I want to show the standard sidebar.

I’ve created a new sidebar in functions.php. Like this:

register_sidebar( array(
				'name' => __( 'Dog Sidebar', 'sparkling' ),
				'id' => 'sidebar-2',
				'description' => __( 'Extra sidebar', 'sparkling' ),
				'before_widget' => '<aside id="%1$s" class="widget %2$s">',
				'after_widget' => '</aside>',
				'before_title' => '<h3 class="widget-title">',
				'after_title' => '</h3>',
	) );

This shows the sidebar area under appearance and I’m able to fill it with widgets.

I then created a category specific page called category-dogs.php and in that file i try to call my new sidebar through:

<?php get_sidebar('sidebar-2'); ?>

The problem is that my extra sidebar only appears if the standard sidebar that comes with the theme is empty and doesn’t contain any widgets.

I’ve also created a sidebar-2.php file but I’m not sure if I need it and what I need to change in this file. At the moment I have pasted the code from sidebar.php into it.
In the sidebar.php file there is the following code
<?php if ( ! dynamic_sidebar( ‘sidebar-1’ ) ) : ?>

I tried to change this to sidebar-2 in the sidebar-2.php file but because I’m not sure what is happening in this file I can’t get it to show up on the front end.

You might want to look into WooSidebars plugin which will help you to create sidebar without writing your own code.

The one thing I can see without testing your code is that you need to create sidebar-2.php and add
<?php if ( ! dynamic_sidebar( 'sidebar-2' ) ) : ?> in it to call for your newly created sidebar.

Another thing is that your <?php get_sidebar('sidebar-2'); ?> should be like this

<?php get_sidebar('2'); ?> because your new sidebar file is created like sidebar-2.php.

If you would create sidebar-dog.php then function to call for sidebar would be <?php get_sidebar('dog'); ?> Because WordPress will understand that you are talking about sidebar, you just ask for the right one.