WordPress Conditional Sidebar Section Based off of Page ID

Goal – have a section of my sidebar that is conditional depending on what page I’m on. All child pages of the selected page should also show the conditional section.

There are at least two ways to do this – 1) have all the conditional hooks and resulting code in one sidebar file and, 2) have multiple sidebar.php documents and call them based off what pages are being visited.

Read More

I’ve pretty much failed at both ways so far… 🙁 I’m working on the second method right now though, because I think it might be more flexible long term. Anyway, in my page.php I replaced:

<?php get_sidebar() ?> 

With

<?php
         if (is_page(1997) || $post->post_parent) {
 get_sidebar('sidebar-test-may12.php')
}
        else { get_sidebar()
        }   ?>

With the hope of testing the method. What I want to happen is that if the page is page id 1997 or any of its child pages please show sidebar-test-may12.php. Otherwise show the regular sidebar. With the new code the whole page goes blank. Any suggestions or solutions for me? Seems like a common problem, but I haven’t been able to figure it out yet. Thanks!

Related posts

Leave a Reply

3 comments

  1. You have a few problems with your code example:

    1) $post->post_parent is not being checked against anything else, so it will only return true if it is a child page (not necessarily the child of the page you want to target)

    2) get_sidebar() is being called incorrectly. If you want to get ‘sidebar-test-may12.php’ from your theme folder, you need to call get_sidebar(‘test-may12’)

    3) You’re missing semicolons after your function calls

    So your code should look like this:

    <?php
        if(is_page(1997) || $post->post_parent == 1997) {
            get_sidebar('test-may12'); //get sidebar-test-may12.php
        }
        else{
            get_sidebar(); //get sidebar.php
        }
    ?>
    

    Let me know if that helps.

    UPDATE: Bear in mind, $post->post_parent does NOT get the top-most ancestor ID of a child page. If you want to grab the top-level ID regardless of depth, consider doing this:

    <?php
        $ancestors = get_ancestors(get_the_ID(), 'page');
        if(is_page(1997) || end($ancestors) == 1997)
            get_sidebar('test-may12'); //get sidebar-test-may12.php
        else
            get_sidebar(); //get sidebar.php
    ?>
    

    POSSIBLE SOLUTION: Building off your example and my proposed ancestry check, one thing you can do is have your template check to see if a special sidebar exists in your theme based on the parent page’s slug. This way, if you decide a particular page needs a special sidebar for it and all of its children/grandchildren/great-grandchildren/etc. you just need to add it into your theme with a name of ‘sidebar-{parent_slug}.php’. So:

    <?php
    $id = get_the_ID();
    $ancestors = get_ancestors($id, 'page');
    $top_page = $ancestors ? get_page(end($ancestors)) : get_page($id);
    
    if(locate_template('sidebar-'.$top_page->post_name.'.php'))
        get_sidebar($top_page->post_name);
    else
        get_sidebar();
    ?>
    

    This way, you don’t need a ton of conditionals to decide which Sidebar file to load on a general Page template.