WordPress.org

Ready to get started?Download WordPress

Forums

Check page for child pages (2 posts)

  1. hig116
    Member
    Posted 1 year ago #

    Hey guys,

    I have a site where I have some pages who have subpages. I need to list those subpages and the parent page. I want to do this conditionally so that the parent page doesn't get list on a page without children.

    Here is my code:

    function has_children($post_id) {
        $children = get_pages("child_of=$post_id");
        if( count( $children ) != 0 ) { return true; } // Has Children
        else { return false; } // No children
    }
    
    add_action ('genesis_before_post_title', 'subpage_nav');
    	function subpage_nav(){
    
    		if ( has_children($post->ID) ) {
    
    		echo '<div id="pagenav">' . '<ul class="clearfix">';
    		wp_list_pages( array('title_li'=>'','include'=>get_post_top_ancestor_id()) );
    		wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>get_post_top_ancestor_id()) );
    		echo '</ul>' . '</div>';
    	   }
    
       else;
    
    	};

    This is listing the parent and child page. What am I doing wrong.

    Thank you,
    Keith

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

    This is listing the parent and child page.

    nothing wrong, this is how it is programmed in these two lines:

    wp_list_pages( array('title_li'=>'','include'=>get_post_top_ancestor_id()) );
    wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>get_post_top_ancestor_id()) );

    top line is for showing the parent, bottom line is for showing the children.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.