How to display a sidebar only for blog?

I have 2 sidebars(primary and secondary). I want the secondary sidebar to display only on the blog page and primary sidebar to display everywhere else except for the blog page. What have I tried:

In functions.php :

if (is_page('blog-35')) {
    get_sidebar('secondary');
} else {
    get_sidebar('primary');
}

Unfortunately this doesn't work. Any hints on how to debug this? I don't understand what am I doing wrong in the code above.(blog-35 is the body class of the page I want to display only the secondary sidebar)

Topic sidebar Wordpress

Category Web


Same thing I did on the athemeart blog page and below is the code

if( is_home() && is_active_sidebar( 'athemeart-sidebar' )  ):
  dynamic_sidebar( 'athemeart-sidebar' );
endif;

Thanks


First: is_page() expects a page id, so the call would be is_page(35).

Second: get_sidebar needs to be called from the template (not the functions file).

You probably already have two templates setup:

  • page.php - In here you'll call get_sidebar('primary');

  • index.php (assuming your blog is on the home page) - In here you'll call get_sidebar('secondary');

Please let me know if this answers your questions!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.