Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all child pages of a parent page in wordpress?

Example:

About
--- Menu 1
--- Menu 2
--- Menu 3
--- Menu 4

if i'm in about page... i have the sub pages. but, if enter to Menu 1 all the pages disappear

What i need is all the time see the parent pages

Currently I have this code

<? if (is_page()) {
    $g_page_id = $wp_query->get_queried_object_id();
    wp_list_pages("depth=4&title_li=&child_of=".$g_page_id."&sort_column=menu_order");
   }
?>

Thanks!

Resolved

i use this and work fine!

<?php
if ( is_page() ) :
    if( $post->post_parent ) :
        $children = wp_list_pages( 'title_li=&child_of='.$post->post_parent.'&echo=0' );
    else;
        $children = wp_list_pages( 'title_li=&child_of='.$post->ID.'&echo=0' );
    endif;
    if ($children) : ?>
        <div class="title">
            <?php
            $parent_title = get_the_title( $post->post_parent );
            echo $parent_title;
            ?>
            <span></span>
        </div>
        <ul>
            <?php echo $children; ?>
        </ul>
    <?php
    endif;
endif;
?>
like image 347
krathos Avatar asked May 10 '13 18:05

krathos


People also ask

How do I display a list of child pages for a parent page in Confluence?

From the editor toolbar, choose Insert > Other Macros. Choose Children Display from the Confluence content or Navigation category. Use the parameters below to specify which pages to display, and how you want them to look. Choose Insert.

How do I find my child's current page on WordPress?

php if ( is_page() ) : if( $post->post_parent ) : $children = wp_list_pages( 'title_li=&child_of='. $post->post_parent. '&echo=0' ); else; $children = wp_list_pages( 'title_li=&child_of='. $post->ID.

How do I get all the pages on WordPress?

Use the get_pages function of wordpress.


1 Answers

Here you go. A bit late for the author, but people will come here for an answer still ;-)

<?php 
// determine parent of current page
if ($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $parent = $ancestors[count($ancestors) - 1];
} else {
    $parent = $post->ID;
}

$children = wp_list_pages("title_li=&child_of=" . $parent . "&echo=0");

if ($children) {
?>

    <ul class="subnav">
        <?php 
            // current child will have class 'current_page_item'
            echo $children; 
        ?>
    </ul>

<?php 
} 
?>
like image 88
Jos Faber Avatar answered Sep 19 '22 03:09

Jos Faber