Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out how to get to a is_child() equivalent; I'd like to be able to target the parent page and it's children. How can this be achieved?

My pseudo-code is...

if (is_page('pagename') || is_child('pagename')) {...}

...however, the is_child() doesn't seem to work. I would like it to return true.

share|improve this question
1  
What exactly should it do? Return TRUE if the current page or the page with that slug is a child page? –  toscho May 29 '13 at 22:40
    
Return true, sorry. I'd like to use it to filter the menu based on the page... for example currently I can... if (is_page('home')) { wp_nav_menu( array( 'theme_location' => 'main-menu', 'container_class' => 'mainMenu' ) ); } ...which works, but I can't figure out an is_page('home) || is_child('home') type of setup –  beta208 May 29 '13 at 22:44

3 Answers 3

up vote 0 down vote accepted

To check for a specific parent ID use $post->post_parent=="123" and replace "123" with the parent ID of your choice.

I learned how to do this by using the Widget Logic plugin. From the Widget Logic notes:

global $post; return (is_page('pagename') || ($post->post_parent=="13")); -- home page OR the page that's a child of page 13

Edit: A better source is Testing for sub-Pages in the codex.

share|improve this answer
    
I could be using it wrong, but does this also work for pages, not posts? I'm trying to reduce code as much as possible by condensing it into one conditional... if (is_page('pagename') || ($post->post_parent=='pagename')) didn't return errors, but also did not work on the child pages –  beta208 May 29 '13 at 22:53
    
It may require that you use the actual parent ID instead of the pagename. Have you tried that? Also try removing the parenthesis around ($post->post_parent=='pagename') as they don't appear to be necessary here. See snippet 3 in the codex which uses this syntax if ( is_page( 'about' ) || '2' == $post->post_parent ) –  RyanLoremIpsum May 29 '13 at 22:59
1  
@beta208 Posts don't have parents, so it doesn't make sense for to work with posts... –  Stephen Harris May 29 '13 at 23:01
    
Also tried if (is_page('pagename') || 'pagename' == $post->post_parent) {...} Which didn't work; I'd love to use the ID, but I'm not seeing it. When I click Quickedit see the title, the slug, date, author, password, parent, order, and template. –  beta208 May 29 '13 at 23:04
    
The snippet 3 looks like my solution, it may just be that I'm needing to find the equivalent of the ID for my pages –  beta208 May 29 '13 at 23:07

Searching for this function to use with Widget Logic, I found this snippet, adapted bellow:

/**
 * Check if a page is child of another 
 * 
 * @param mixed $page_id_or_slug Provide the parent ID or Slug
 * @return bool Whether the page is child or not of the given parent
 */
function is_child( $page_id_or_slug ) 
{ 
    global $post; 
    if( !is_page() )
        return false;

    # Slug provided, get ID
    if( !is_int( $page_id_or_slug ) ) 
    {
        $page = get_page_by_path( $page_id_or_slug );
        $page_id_or_slug = $page->ID;
    } 

    return $post->post_parent == $page_id_or_slug; 
}
share|improve this answer

This will check for descendancy rather than strict parent-child relationship:

/**
 * Is the current page a descendent of page identified by the path (not slug).
 * A page is not a descendent of itself!
 * @param string $page_path The path of the page, e.g. mypage/mysubpage 
 * @return boolean True if current page is a descednent of specified $page_path. False otherwise.
 */
function is_descendent_of( $page_path ){

    if( !is_page() ){
        return false;
    }

    $ancestors = get_post_ancestors( get_queried_object_id() );
    $page = get_page_by_path( $page_path );
    if( $page )
        return in_array( $page->ID, $ancestors );
    return false;   
}

And for strict parent-child checking:

/**
 * Is the current page a child of the page identified by the path (not slug).
 * A page is not a child of itself! Child != descendant.
 * @param string $page_path The path of the page, e.g. mypage/mysubpage
 * @return boolean True if current page is a child of specified $page_path. False otherwise.
 */
function is_child_of( $page_path ){

    if( !is_page() ){
        return false;
    }

    $page = get_page_by_path( $page_path );
    $current_page = get_post();

    if( $page )
        return $current_page->post_parent == $page->ID;
    return false;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.