Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7890491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:22:05+00:00 2026-06-03T06:22:05+00:00

Goal – have a section of my sidebar that is conditional depending on what

  • 0

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.

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:22:06+00:00Added an answer on June 3, 2026 at 6:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: write a query that can bring back all the users of a company,
Goal: Having two buttons that should be enable to add or delete data from
Goal: I have a Db source. Depending on a variable, i need to store
goal: I have the string 1234432144 I want to only replace the first 2
Goal: I'm trying to refresh content inside a div so that if the user
Goal: json_encode a PHP object that has private properties Send the encoded object as
Goal: Get values which are unique in $resultSecond So I have two objects. $result
Goal: Rolling/Running total for all statements at the end of each month. Code: select
Goal I have a generic class GenericClass<T> and I want to pool instances. I'm
Goal: Retrieve all instance of List<> from interface GameFactory Problem: I retrieve error message

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.