Title: Conditional Sidebar Based on Parent
Last modified: August 19, 2016

---

# Conditional Sidebar Based on Parent

 *  [cspalmisano](https://wordpress.org/support/users/cspalmisano/)
 * (@cspalmisano)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/)
 * 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)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/#post-981812)
 * 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](https://wordpress.org/support/users/cspalmisano/)
 * (@cspalmisano)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/#post-981822)
 * 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
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/#post-981840)
 * 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](https://wordpress.org/support/users/cspalmisano/)
 * (@cspalmisano)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/#post-981844)
 * 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](https://wordpress.org/support/users/cspalmisano/)
 * (@cspalmisano)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/#post-981845)
 * Just to clarify, I’m not showing the same sidebar on the child pages as the parent
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/#post-981882)
 * 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](http://codex.wordpress.org/Include_Tags) [Page Templates](http://codex.wordpress.org/Pages#Page_Templates)

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

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [17 years, 4 months ago](https://wordpress.org/support/topic/conditional-sidebar-based-on-parent/#post-981882)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
