0

I am developing a theme and using WP_Query to fetch the content from the database, but now i wanted to implement a loop using a WP_query that will show me the next 10 posts with title links and featured images while on the single page of a post . for example

http://news.distractify.com/geek/hola-bonjour-marhaban/

In this example , as you open the post named "13 Fun Ways To Make It Easier To Learn A New Language", In the sidebar , it shows the next 5 posts and in the bottom after the end of the current post content it shows us the latest posts after the current post you are currently on.

0

This will get you next 10 posts on single post template.

<?php

    global $post;
    $current_post = $post;

    for ( $i = 1; $i <= 10; $i++ ) {
        $post = get_previous_post(); // this uses $post->ID
        setup_postdata($post);

        get_template_part( 'content', get_post_format() );

    }

    $post = $current_post; // restore

?>
| improve this answer | |
  • I need your another favour, in this code i wanted to get the permalink of the first post and i wanted to echo it outside the condition. – Harman Preet Sep 11 '14 at 6:05
  • You can't call permalink outside loop unles you define post id in it. – Robert hue Sep 11 '14 at 7:32
  • How to load the set of next 10 posts using above code <div class="post-nav-container"><?php previous_posts_link( __('&rarr; Older Posts','circoviral')); ?><?php next_posts_link( __('&larr; Older Posts','circoviral')); ?></div> – Harman Preet Sep 26 '14 at 15:55
  • I am using infinite scroll functionality so , this is very neccessary for me – Harman Preet Sep 26 '14 at 15:56

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.