0

I am currently using the default Page Navigation (wp_list_pages) with wordpress and I have set my homepage to a page called "Home" and my blog to a page called "Events" but I would like to list the 10 most recent posts under the child of "Events".

I attempted to hack it in using the following code but it threw up content all over the place and removed tags:

add_filter('wp_list_pages', 'add_forum_link');
function add_forum_link($output) {
        $output .= '<li><a href="#">Blog</a><ul>';

        query_posts('showposts=10');
        while ( have_posts() ){ the_post();

        $output .= '<li><a href="'.the_permalink().'">'.the_title().'</a></li>';
        }

        $output .= '</ul></li>';
        echo $output;
}

Plus it involved me creating a new link called "Events" and so it doesn't work very well.

Is there an option to just make Wordpress find which page I've set as my Posts and show the last 10 blog posts under that heading?

Any help would be great!!

  • I see you used the wordpress.com tag. Is your site hosted with wordpress.com? – Pieter Goosen Apr 21 '14 at 13:05
  • O no it isn't sorry I messed up – Joseph Gregory Apr 21 '14 at 13:09
0

You've got the right idea, however;

  1. Don't use query_posts, use get_posts instead
  2. Using the wp_list_pages filter will just add the list at the end
  3. Using template tags like the_permalink() will echo the output, so you can't use it in string concatenation

You'll need to use a custom walker (the family of classes for generating hierarchical content), which will allow you to inject your list right after the posts page:

/**
 * Add 10 most recent posts as a child list of the posts page.
 * 
 * @link https://wordpress.stackexchange.com/q/141929/1685
 */
class WPSE_141929_Walker extends Walker_Page {
    function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
        parent::start_el( $output, $page, $depth, $args, $current_page );
        if ( $page->ID == get_option( 'page_for_posts' ) ) {
            $posts = get_posts(
                array(
                    'posts_per_page' => 10,
                )
            );

            if ( $posts ) {
                $output .= '<ul class="children">';     
                foreach ( $posts as $post ) {
                    $output .= '<li><a href="' . get_permalink( $post->ID ) . '">' .get_the_title( $post->ID ) . '</a></li>';
                }
                $output .= '</ul>';
            }
        }
    }
}

And in use:

wp_list_pages(
    array(
        'walker' => new WPSE_141929_Walker,
    )
);
|improve this answer|||||
  • Thats amazing with the exception is their a way to tie that in to my current <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?> as all my other code links in with that? – Joseph Gregory Apr 21 '14 at 13:13
  • Sorry just clicked <?php wp_nav_menu( array( 'theme_location' => 'primary','walker' => new WPSE_141929_Walker ) ); ?> – Joseph Gregory Apr 21 '14 at 13:15

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.