Support » Fixing WordPress » Conditional Tags and Parent/Child pages

  • Is it possible to use conditional tags to cover all the pages that have a particular parent? For example, if I have a parent page with the slug “about” and I create three child pages of “about” named “vita” “contact” and “portfolio”, how would I use conditional tags to cover all of these pages. Using if is_page(‘about’) doesn’t do the trick.

    Basically, I want to change the content of my sidebar based on what parent the child page is under. Here’s my script for the sidebar:

    <div id=”sidebar”>

    <!– PHP includes for the sidebar…uses different include files depending on what part of the site is being viewed. –>

    <?php if (is_home()) {
    include (“inc/home_side.php”);
    } elseif (is_archive()) {
    include (“inc/archive_side.php”);
    } elseif (is_page(‘work’)) {
    include (“inc/work_side.php”);
    } else if (is_page(‘archives’)) {
    include (“inc/archives_side.php”);
    } else if (is_page(‘about’)) {
    include (“inc/about_side.php”);
    } elseif (is_page(‘links’)) {
    include (“inc/linksside.php”);
    } ?>

    So, I want all of the child pages of the “about” page to use the include file “about_side.php” for the sidebar…make sense? Right now it only uses the include file for the “about” page.

    I did a search, and all I could find relevant is here. I’m familiar with using conditional tags. I know I could just write an if is_page() for every page I make, but I’d rather cover it by just specifying the parent page.

    Any suggestions?

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you need to backtrack to the parent of a Page, you’ll have to enhance your conditional testing. See this post (#1) for a way to capture the post (or rather, page) name of a Page’s parent:

    With that you could then modify your code:

    } else if (is_page('about') || 'about' == $parent_name ) {
    include ("inc/about_side.php");

    Or if only the child Pages are to use about_side.php:

    } else if (!is_page('about') && 'about' == $parent_name ) {
    include ("inc/about_side.php");

    Awesome! Getting something to work in PHP just makes me want to learn more. Thanks!

    For anyone interested, a phps file of the final result, using Kafkaesqui’s suggestions can be found here.

    Just what i was looking for!

    Thanks 😉

    This is fantastic. Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Conditional Tags and Parent/Child pages’ is closed to new replies.