WordPress.org

Forums

get_post_ancestors with wp_query (2 posts)

  1. papergecko
    Member
    Posted 4 years ago #

    Hi.

    I'm building a site using WordPress as a CMS and which uses pages almost exclusively to organise content.

    I'm trying to find a way of creating a navigation menu which lists all the child and grandchild pages of top level pages, so the menu stays the same as you browse child and grandchild pages. There's a couple of solutions around which work perfectly when using wp_list_pages and get_post_ancestors but I would like to include text excerpts from the pages too, and for that I think I need to use a wp_query.

    I wonder if someone can help?

    The wp_list_pages version of the code is as follows:

    <?php
    global $wp_query;
    $post = $wp_query->post;
    $ancestors = get_post_ancestors($post);
    if( empty($post->post_parent) ) {
        $parent = $post->ID;
    } else {
        $parent = end($ancestors);
    }
    if(wp_list_pages("title_li=&child_of=$parent&echo=0&sort_column=menu_order" )) { ?>
    
    <h3><?php $page = get_post($parent);
    echo $page->post_title; ?></h3>
    
    <ul id="secondary-nav">
        <?php wp_list_pages("title_li=&child_of=$parent" ); ?>
    </ul><!-- #secondary-nav -->
    
    <?php } ?>

    (code adapted from this post)

    Essentially I'm trying (and failing) to replace the second wp_list_pages bit with code which enables me to include text excerpts as well as just page titles and permalinks.

    Any help much appreciated!

  2. jimmyt1988
    Member
    Posted 4 years ago #

    the_excerpt();

Topic Closed

This topic has been closed to new replies.

About this Topic