0

I know that a list of posts is generated automatically in WordPress, but what I have been looking for is to have a summary of pages, possibly in a hierarchic format, that resides at the side of a certain page. Here is an example: http://www.w3schools.com/js/js_variables.asp

That website does not use Wordpress though, but that's the kind of functionality I am looking for. I know how to assign a parent page when creating a new page, but I don't know how to generate a summary and put it on the side. Do I need any specific plugin for this?

1

You can use wp_list_pages with the child_of argument:

/**
 * Display hierarchical list of all posts for a given post.
 * 
 * @param   int|WP_Post $post   Display for post. Defaults to current post/page.
 */
function wpse_184554_list_section( $post = null ) {
    if ( ! $post = get_post( $post ) )
        return;

    if ( $post->post_parent && $ancestors = get_post_ancestors( $post ) )
        $parent_id = array_pop( $ancestors );
    else
        $parent_id = $post->ID;

    echo '<ul class="pages">';
        wp_list_pages(
            array(
                'post_type' => $post->post_type, // Will work for any hierarchical post type
                'child_of'  => $parent_id,
                'title_li'  => '',
            )
        );
    echo '</ul>';
}

This will output a hierarchical list of all pages that are children of the current page's top level parent (or simply all child pages if the current page has no parent).

Pop it in your functions.php. To use, call it in any template file, for example:

<div class="sidebar">
    <?php wpse_184554_list_section() ?>
</div>
  • Looks like a great solution. I would greatly appreciate if you could add some more beginner-friendly information to your answer, such as where do I insert this code or what do I need to change from it? – multigoodverse Apr 17 '15 at 14:06
  • This code will output the list - it might be a better idea to wrap it in a function so you can call it anywhere in your template. See revision. – TheDeadMedic Apr 17 '15 at 14:12
  • I inserted the first code snippet in the function.php file, and the second snippet in the sidebar, but nothing happened. I also tried to insert the second snippet in the page.php template, but again nothing. – multigoodverse Apr 19 '15 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.