• I would like to construct at statement that says:

    If the parent of the current page is ID 1 show sidebar01.php
    else
    If the parent of the current page is ID 2 show sidebar02.php
    else
    If the parent of the current page is ID 3 show sidebar03.php

    I have reviewed the support section on conditionals and cannot figure out how to do this.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Might change your sidebar file names to sidebar-1.php etc. as get_sidebar('1') will include sidebar-1.php

    So then you would only need this:

    get_sidebar($post->ID);

    But if you want the if stuff

    if ($post->ID == '1') {
    //do what you need to display sidebar 1
    }
    if ($post->ID == '2') {
    //do what you need to display sidebar 2
    }
    if ($post->ID == '3') {
    //do what you need to display sidebar 3
    }

    or

    if($post->ID == "1") {
      echo "do sidebar 01 thingy";
    } elseif($post->ID == "2") {
      echo "do sidebar 02 thingy";
    } elseif($post->ID == "3") {
      echo "do sidebar 03 thingy";
    } else {
      echo "do default sidebar thingy";
    }

    Thread Starter cspalmisano

    (@cspalmisano)

    Michael H:
    I’m not looking to display a sidebar based on the current page post ID. Rather, the parent of the current page’s ID.

    Thanks

    Oh you did say that didn’t you? Oops!

    Then try:
    `
    if ($post->post_parent) {
    get_sidebar($post->post_parent);
    } else {
    get_sidebar();
    }

    Assumes you follow this advice:

    Might change your sidebar file names to sidebar-1.php etc. as get_sidebar('1') will include sidebar-1.php

    Thread Starter cspalmisano

    (@cspalmisano)

    Makes sense but where in that code do i specify the id of the parent page?

    I need to be able to say: If parent page id=1 show sidebar 1.

    Thread Starter cspalmisano

    (@cspalmisano)

    Just to clarify, I’m not showing the same sidebar on the child pages as the parent

    Tell you what. Do this. I am going to assume that one of your parent pages had the ID number of 7. If you don’t have a parent page with ID 7, then where I refer to 7, use YOUR parent page ID.

    1.Change to the WordPress Default theme.
    2.Copy wp-content/themes/default/sidebar.php to wp-content/themes/default/sidebar-7.php.
    3.In wp-content/themes/default/sidebar-7.php, change this code:

    <?php wp_list_pages('title_li=<h2>Pages</h2>' ); ?>

    to this:

    <?php echo '!!!!This is sidebar-7.php calling!!!! '; ?>
    <?php wp_list_pages('title_li=<h2>Pages</h2>' ); ?>

    4. In wp-content/themes/default/pages.php, change this code:

    <?php get_sidebar(); ?>

    to this:

    <?php
    if ($post->post_parent) {
    get_sidebar($post->post_parent);
    } else {
    get_sidebar();
    }
    ?>

    For your review:
    Include_Tags
    Page Templates

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Conditional Sidebar Based on Parent’ is closed to new replies.