WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Depth parameter not working; like to display children and *not* grandchildren (3 posts)

  1. abitofbrit
    Member
    Posted 1 year ago #

    I'm trying to display the children of a post and not the grandchildren of the post. I've been trying to set the depth parameter, but for the life of me, I can't get it to work. What am I missing?

    <?
    
    global $post;
    
    $ancestors = get_post_ancestors( $post->ID);
    
    $ancestor = $ancestors[0];
    
    $children_args = array(
    	"child_of" => $ancestor,
    	'sort_column' => 'menu_order',
    	'depth'        => 1
    );
    
    $children = get_pages($children_args);
    
    ?>
    
    <ul class="ancestor_menu">
    							<?php
    
    							foreach ( $children as $child ):
    
    								global $wp_query;
    
    								$postID = $wp_query->post->ID;
    
    								if($postID === $child->ID) {
    
    									$active = ' class="current-menu-item"';
    
    								};
    
    							?>
    								<li<?=$active?>><a href="<?php echo $child->guid; ?>"><?php echo $child->post_title; ?></a></li>
    							<?php
    
    							unset($active);
    
    							endforeach;
    
    							?>
    							</ul>
  2. keesiemeijer
    moderator
    Posted 1 year ago #

    I don't think there is a depth parameter for get_pages().
    http://codex.wordpress.org/Function_Reference/get_pages

    Try it with the 'parent' argument [untested].

    $children_args = array(
    	"parent" => $ancestor,
    	'sort_column' => 'menu_order',
    	'hierarchical' => false,
    );
  3. abitofbrit
    Member
    Posted 1 year ago #

    Hah... that could be why it wasn't working. :)

    Your suggestion worked. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic