1

I have two sidebars registered. One called Sidebar (sidebar-1) and the other called Blog Sidebar (sidebar-2).

I would like to display the Blog sidebar on the blog index and single posts. As WordPress doesn't have a conditional tag for the blog page, I have tried the following via sidebar.php:

<?php
if ( is_front_page() && is_home() ) {
  // Default homepage - Do nothing
} elseif ( is_front_page() ) {
  // Static homepage - Do nothing
} elseif ( is_home() && is_single() ) {
  // Display blog sidebar
?>

    <aside>
        <?php dynamic_sidebar( 'sidebar-2' ); ?>
    </aside>

<?php
    } else {
      // Display main sidebar
?>

    <aside>
        <?php dynamic_sidebar( 'sidebar-1' ); ?>
    </aside>

<?php } ?>

When I go to the blog index and single post, I'm expecting to see the Blog Sidebar (sidebar-2), but I'm seeing the Sidebar (sidebar-1) instead.

Via reading settings, I have selected a page for my blog posts.

1 Answer 1

3

There is no conditional tag for the blog page. You have to use both is_home() and is_front_page() to detect this page

When you use is_home() and is_front_page(), you have to use them in the right order to avoid bugs and to test every user configuration:

<?php
if ( is_front_page() && is_home() ) {
 // blog listings
} elseif ( is_front_page() ) {
 // static homepage
} elseif ( is_home() ) {
 // blog page
} else {
 //everything else
}
?>

Taken from https://codex.wordpress.org/Conditional_Tags

If you want to check it another way, the posts / frontpage is stored in the database as an option which we can get the data using get_option which we can use:

get_option('show_on_front');

We can then do a check on that like so:

$front_page_type = get_option('show_on_front');
if($front_page_type = 'posts') {
 // blog page
} else {
 // static front page
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.