Forums

[resolved] using is_page with children pages? (21 posts)

  1. mzimmers
    Member
    Posted 4 years ago #

    I've read up on the is_page() tag, and I can see how to choose a banner image based on what Page is displaying. What I'd *really* like to do, though, is to choose a banner image if a child Page is displaying as well. So, for example, I'd like image3.jpg to display if I'm currently on Page 3, Page 3.1, Page 3.2, etc.

    Is there an efficient way to do this, or do I have to hard-code each Page number into a separate test?

    Thanks...

  2. MichaelH
    Volunteer
    Posted 4 years ago #

    Something like:

    if ($post->post_parent == 3)

    Of course the 3 is the ID of that post (page) (post_type='page').

  3. Kafkaesqui
    Moderator
    Posted 4 years ago #

    Try this:

    <?php global $wp_query; if( (3 == $wp_query->post->ID) || (3 == $ $wp_query->post->post_parent) ) :?>
    ~DO THIS IF PAGE OR PAGE PARENT IS PAGE 3~
    <?php endif; ?>

    That will cover both Page 3 *and* any child Pages of 3.

  4. mzimmers
    Member
    Posted 4 years ago #

    Thanks for the suggestions. One question: I do notice the term "post" in both of your code stubs above. I need this for Pages, not posts. Is there something equivalent that will work?

    Thanks again.

  5. Adam Brown
    Member
    Posted 4 years ago #

    That's not a mistake. As far as the database is concerned, everything is a "post" (even pages).

  6. mzimmers
    Member
    Posted 4 years ago #

    Really -- that's very useful information for the future. Thanks for the clarification. I'll try this code a little later on today, and will report back.

  7. Kafkaesqui
    Moderator
    Posted 4 years ago #

    "As far as the database is concerned, everything is a "post" (even pages)."

    Well, that is until you hit the 'post_type' column... ;)

  8. Otto
    Tech Ninja
    Posted 4 years ago #

    Why not make it really clever?

    function has_parent($post, $post_id) {
      if ($post->ID == $post_id) return true;
      else if ($post->post_parent == 0) return false;
      else return has_parent(get_post($post->post_parent),$post_id);
    }

    This will check any number of levels. If the post, it's parent, it's grandparent, etc, has the given ID, it returns true.

    Usage:

    <?php global $wp_query; if( has_parent($wp_query->post), 3)) :?>
    ~DO THIS IF PAGE OR PAGE PARENT OR GRANDPARENT IS PAGE 3~
    <?php endif; ?>
  9. mzimmers
    Member
    Posted 4 years ago #

    Wow...that is really slick. I'll need that, too, since I will have three levels of Pages on my site.

    I almost feel guilty hard-coding in Page numbers into code so clever. Do people ever define constants for things like Page numbers to make the code a bit more maintainable?

  10. Otto
    Tech Ninja
    Posted 4 years ago #

    Do people ever define constants for things like Page numbers to make the code a bit more maintainable?

    Not usually, no, but you can do so if it makes you feel better. :)

    <?php define('MY_PAGE',3); ?>

  11. soundproofskin
    Member
    Posted 4 years ago #

    Hi Otto or anyone out there, the description in the post above is exactly what I need for a website I am working on -

    "This will check any number of levels. If the post, it's parent, it's grandparent, etc, has the given ID, it returns true."

    Unfortunately, being a designer and not much of a coder, I'm really struggling to implement the code into my site. Here's the code I have at the moment which displays a different heading depending on which page the user is on:

    <h1>
    <?php if (is_page(13)) echo "Welcome to the website" ?>
    <?php if (in_category(12)) echo "News and Updates" ?>
    <?php if (in_category(13)) echo "On Call" ?>
    <?php if (is_page(2) || $post->post_parent=="2") echo "About us"	?>
    </h1>

    The about us page has children and grandchildren. I basically want the title to remain the same on the page if you are a couple of levels in. Can anyone help me incorporate the code above into my code?

  12. soundproofskin
    Member
    Posted 4 years ago #

    Bump!

  13. Ivovic
    Member
    Posted 4 years ago #

    you'll basically have to loop through the parents until you reach the top.

    put a function like the following into your functions.php in your template...

    function is_superparent($parentid) {
    	if (is_page('2')) return true;
    	else {
    		while ($parentid) {
    			$page = get_page($parentid);
    			$parentid  = $page->post_parent;
    			if ($parentid=="2") return true;
    		}
    		return false;
    	}
    }

    Pretty sure that'll work...

    Then in your page template you can do:

    <?php if (is_superparent('2')) : ?>

    stuff to doif the parent of the parent of the parent of the parent is ID 2...

    <?php endif; ?>

    try that.

  14. soundproofskin
    Member
    Posted 4 years ago #

    Thanks for getting back to me.

    I've tried playing with that code but the best I can get is for it to work on the actual page with the ID of '2' - the children and children thereof are blank. Here's exactly what I have done...

    In my header file:

    <?php if (is_superparent('2')) : ?>
    <h1>About us</h1>
    <p>The essentials about our purpose, who we work with, how we're organised, our policies, history - and how to contact us when it's not an emergency.</p>
    <?php endif; ?>

    In my functions.php:

    function is_superparent($parentid) {
    	if (is_page('2')) return true;
    	else {
    		while ($parentid) {
    			$page = get_page($parentid);
    			$parentid  = $page->post_parent;
    			if ($parentid=="2") return true;
    		}
    		return false;
    	}
    }

    Any idea what's wrong?

  15. soundproofskin
    Member
    Posted 4 years ago #

    I've made some progress - I can get it almost to work! The problem is that it is now showing all the different intoductions under one another, rather than just showing the one according to the page/child I am on.

    Here's the header.php code:

    <!-- About us -->
    <?php if (is_superparent('2')) : ?>
    <h1>About us</h1>
    <p>The essentials about our purpose, who we work with, how we're organised, our policies, history - and how to contact us when it's not an emergency.</p>
    <?php endif; ?>
    
    <!-- Hill safety -->
    <?php if (is_superparent('23')) : ?>
    <h1>Hill safety</h1>
    <p>Prevention is better than rescue.  We have put together some resources for your use on this page to help you be self reliant in wild and remote places.</p>
    <?php endif; ?>
    
    <!-- Support us -->
    <?php if (is_superparent('16')) : ?>
    <h1>Support us</h1>
    <p>As a charity wholly serviced by unpaid volunteers, we rely on your all round support to keep operating.  These are the different ways you can contribute.</p>
    <?php endif; ?>

    Here's the functions.php code:

    function is_superparent($parentid) {
    	if (is_page('2')) return true;
    	else {
    		while ($parentid) {
    			$page = get_page($parentid);
    			$parentid  = $page->post_parent;
    			if ($parentid=="2") return true;
    		}
    		return false;
    	}
    }

    Can anyone help?

  16. Mark
    Member
    Posted 4 years ago #

    I have posted about a similar problem here.

    This isn't an issue on any of my other sites, only now in WP2.5

    the is_page() function should work regardless of whether its a top level page, sub page, sub-sub page - they're all pages, so this function should work...

    thoughts?

  17. Mark
    Member
    Posted 4 years ago #

    anyone?

  18. Otto
    Tech Ninja
    Posted 4 years ago #

    is_page() does work just fine. I'm not sure what you're asking here.

  19. Mark
    Member
    Posted 4 years ago #

    otto, try using is_page() on a page that has children, and/or that is a child...

    doesn't work...

  20. kalikat
    Member
    Posted 4 years ago #

    I'm tearing my hair out over the same issue - has anyone gotten this to work? I just can't get WP 2.5 to recognize parent/child/grandchild pages properly.

    I need something that works likes Otto's example above, but I get a parse error when using that exact example. It's on line 29, which is:

    <?php global $wp_query; if( has_parent($wp_query->post), 5)) :?>

    Anyone know what I'm doing wrong?

  21. nosferatuscoffin
    Member
    Posted 3 years ago #

    Well for one thing, you have an extra parentheses at the end of the IF statement.

    It should read:

    <?php global $wp_query; if(has_parent($wp_query->post), 5) :?>

Topic Closed

This topic has been closed to new replies.

About this Topic