is_subpage() – Custom Conditional Function

This function has been updated and improved. Click here to go to the newer version.

— Quick conditional function for WordPress to check if the current page is a sub page. It works both inside and outside the main loop, only for 2.5 and upwards.

→ Download the function here

Example

Same way as you use is_page, is_home, etc.

<?php if (have_posts()) : ?>

	<?php while (have_posts()) : the_post(); ?>

		<?php if ( is_subpage() ): ?>
			<p> This is a subpage </p>
		<?php endif ?>

	<?php endwhile; ?>

The Function


function is_subpage()
{
	global $post, $wpdb;

	if ( is_page() AND isset( $post->post_parent ) != 0 )
	{
		$aParent = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d AND post_type = 'page' LIMIT 1", $post->post_parent ) );
		if ( $aParent->ID ) return true; else return false;
	}
	else
	{
		return false;
	}
}

Im not sure why WordPress didn’t include a function like this by default, i hope you find it useful.
Cheers!

Tags: , ,

Saturday, November 29th, 2008 WordPress

→ If you find this post useful please consider inviting me a cup of tea :) Thanks!

27 Responses to “is_subpage() – Custom Conditional Function”

  1. It does look very simple and useful. You could always submit it to the WP repository so that it might make it’s way into a future release. The tool is only as good as those who create it and those it inspires to continue that process.

  2. JamieO on November 30, 2008.
  3. I would assume that the reason it’s not a function is because it’s easier and more efficient to use !is_home() than to write another function that basically does the same thing.

  4. Trevor Davis on November 30, 2008.
  5. Thanks both for the comments.

    @JamieO, will give it a try thanks ;)

    @Trevor !is_home() only lets you know if you are in a location that it’s not the home page. Could be a tag page, a search page, a category page and even a single page. It wont tell you if your are specifically in a sub page, which that is what this function does.

  6. Matt on December 1, 2008.
  7. @Matt-
    Ah ok, I guess I didn’t look close enough at the function to see what it does. It looks like they have updated the Codex recently, and it tells you how to test for a subpage

  8. Trevor Davis on December 1, 2008.
  9. @Trevor no problem, thanks for the link.

  10. Matt on December 1, 2008.
  11. Hey Matt, cool stuff!

    You should consider making one big page for all of these, and having a download with “Matt Varones WordPress functions.php” or something to that extent :)

  12. Joost de Valk on December 1, 2008.
  13. @Joost Thanks, glad you like it. Sounds like a good idea, thanks for the tip as I might have to do something like that.

  14. Matt on December 1, 2008.
  15. Thanks a lot for this, I feel its shoddy work on the wordpress development team for not already having this function included within the source.

  16. Simon on December 2, 2008.
  17. Thanks very much. It helps me for my new WP theme.

  18. feir on December 23, 2008.
  19. Very nice, it would’ve been helpful a few days ago, I had to develop a folding menu and I needed to test if there was a parent. I did it with get_page($post->post_parent). I hope you don’t mind if I add you to my “Friends” block on my site.
    Regards,

  20. Elliot on January 1, 2009.
  21. Thanks Elliot, glad it might come handy the next time. Please go ahead, would be honored.
    Happy new year!

  22. Matt on January 2, 2009.
  23. and Happy New Year to you too!

  24. Elliot on January 2, 2009.
  25. this was exactly what i needed! thanks matt. will definitely be useful on many a project. :)

  26. ross on February 26, 2009.
  27. Any chance you might know how to make this function so that it can handle is_subpage(8) type of queries?

    In the codex they mention something about testing for a subpage of a particular page using $post->post_parent == ’11′ but I can’t get it to work…

  28. nathan on March 7, 2009.
  29. Hi Nathan. A sub page is stilla page, so If you try checking for both a page ID and subpage it might do the trick for you:


    if ( is_page(8) && is_subpage() )
    {
    // do your stuff here.
    }

    Cheers!

  30. Matt on March 8, 2009.
  31. Nice post. It helped me alot. Thanks,

  32. sam_khrap on March 27, 2009.
  33. @Nathan: the codex gots it, as Trevor mentions above. Here’s their example:

    post_parent == ’2′ ) {
    $bannerimg = ‘home.jpg’;

    } elseif ( is_page(‘learning’) || $post->post_parent == ’56′ ) {
    $bannerimg = ‘teaching.jpg’;

    } elseif ( is_page(‘admissions’) || $post->post_parent == ’15′ ) {
    $bannerimg = ‘admissions.jpg’;

    } else {
    $bannerimg = ‘home.jpg’; // Fall-through
    }

    ?>

    http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages

  34. ross on March 27, 2009.
  35. Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

  36. RaiulBaztepo on March 28, 2009.
  37. Is there any reason not to use the post->ancestors property?

    I did a similar approach and found out later that there is an array in the post objects containing all ancestors. I simply count it’s size:
    If it’s bigger than 0 it’s a subpage, bigger than 1 a subsubpage, etc.

  38. Alex on April 25, 2009.
  39. Hi,

    Thanks for the code – I was wondering how I could check if I’m on a grandchild (sub-sub-page) of page 10 for example. I would be so grateful for any advice. Thanks!

  40. Rebecca on May 7, 2009.
  41. I made a small modification to the function to allow you to check to see if it’s the child of a named page, rather than by its id.

    function is_subpage( $page_name_or_id ) {
    global $post, $wpdb;
    if ( is_page() AND isset( $post->post_parent ) != 0 ) {
    $aParent = $wpdb->get_row( $wpdb->prepare( “SELECT ID, POST_TITLE FROM $wpdb->posts WHERE ID = %d AND post_type = ‘page’ LIMIT 1″, $post->post_parent ) );

    if ( $aParent->ID && ($aParent->ID == $page_name_or_id || $aParent->POST_TITLE == $page_name_or_id) ) return true; else return false;

    } else {
    return false;
    }
    }

    Thanks for the function, Matt!

  42. Rob Williams on July 28, 2009.
  43. Exactly what I was looking for. Thanks for sharing.

  44. Giammarco Schisani on January 10, 2010.

Leave a Reply

About Me

Matt Varone - Matias Varone - sksmatt
HI there,

I'm a freelance creative web developer, UI designer and hobbyist musician.

Twitter Status

Flickr Gallery

    Blue WallClutter drawerCS4 Replacement iconsCS4 Replacement icons