WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Dispaly on Page depth count (9 posts)

  1. Jay
    Member
    Posted 1 year ago #

    The current code outputs the comments template on the first child of its parent.

    $firstChild = get_pages( array( 'child_of' => $post->ID, ) );
    		if ($firstChild ){
    	comments_template();
    	}

    This issue I am having is that the first child also has a child and that child may have a child of its own and so on. I only want the template to display on the first child. Not any grandchildren of the top parent. Hope that makes sense. Thanks in advance for any help. I tried using count, but was not able to achieve my goal. Thanks in advance for any help.

  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    Does $firstChild contain anything that identifies itself as having children?

  3. Jay
    Member
    Posted 1 year ago #

    If I understand correctly I do not believe so. So I need to check my $firstChild variable for children and if it does have children, only display the template on $firstChild. Would that be your suggestion?

  4. Rajesh Soni
    Member
    Posted 1 year ago #

    Try getting the post_parent of the child.
    The children of $post->ID would have post_parent = $post->ID
    The grandchildren post_parent != $post->ID

    Hope that helps.

    RS

  5. Rajesh Soni
    Member
    Posted 1 year ago #

    You could also try to set the depth argument to 1 (to just retrieve the children and NOT grandchildren)

    $firstChild = get_pages( array( 'child_of' => $post->ID, 'depth' => 1) );
    		if ($firstChild ){
    	comments_template();
    	}

    Cheers!

  6. Jay
    Member
    Posted 1 year ago #

    @Rajesh, Thanks. I have actually already tried the 'depth' argument with no luck. I will try out your first suggestions. Thanks

  7. Rajesh Soni
    Member
    Posted 1 year ago #

    @Jay,

    Aah okay. Good luck!

    RS

  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    The current code outputs the comments template on the first child of its parent.

    the way I look at the code, that code displays the comments template of a page, if the page has children (or at least one child).

    I only want the template to display on the first child.

    imho, you could check the page's ancestors, and make the output dependant on the number of ancestors;

    $page_ancestors = get_ancestors($post->ID, 'page');
    		if( count($page_ancestors) == 1 ) {
    	comments_template();
    	}

    http://codex.wordpress.org/Function_Reference/get_ancestors

  9. Jay
    Member
    Posted 1 year ago #

    @alchymyth you always come to the rescue! Works perfectly Cheers!

Topic Closed

This topic has been closed to new replies.

About this Topic