• Resolved mzimmers

    (@mzimmers)


    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…

Viewing 15 replies - 1 through 15 (of 20 total)
  • MichaelH

    (@michaelh)

    Something like:

    if ($post->post_parent == 3)

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

    Kafkaesqui

    (@kafkaesqui)

    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.

    Thread Starter mzimmers

    (@mzimmers)

    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.

    Adam Brown

    (@adamrbrown)

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

    Thread Starter mzimmers

    (@mzimmers)

    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.

    Kafkaesqui

    (@kafkaesqui)

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

    Well, that is until you hit the ‘post_type’ column… ;)

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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; ?>

    Thread Starter mzimmers

    (@mzimmers)

    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?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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); ?>

    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?

    Bump!

    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.

    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?

    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?

    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?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘using is_page with children pages?’ is closed to new replies.