I am using Post contents as a slider contents, i need to make the post infinite number of loop, ie, If i view the last post after that first post should display and before first post the last post.

Please suggest

  • Please provide more details about where you're doing it, any plugins used, some code you're using etc. I'm not even sure if you are talking of server-side or client-side Nov 2, 2012 at 11:09
  • @MridulAggarwal After re-reading the OP question a couple of times I think he refers to looping posts "continuously" so that when you reach the end of the loop (last post), the first post will show again, and so on. Just was worded in a difficult way because it could mean a couple of things..
    – Adam
    Nov 2, 2012 at 11:46
  • I suppose "jquery" tag is not needed if that's the case? Nov 2, 2012 at 11:52
  • 1
    Show us your research efforts.
    – fuxia
    Nov 2, 2012 at 12:16

2 Answers 2


A very basic example,

$args = array(  'post_type' => 'post', 'posts_per_page' => -1 );
$query= new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();

    the_content(); //example, use whatever template functions you like...

wp_reset_postdata(); //reset Post Data
wp_reset_query(); //destroy query incase of using multiple queries on same page


Note, that this is probably an inefficient way to achieve what you want because if your post_type contains many posts it may impede your sites performance because the parameter posts_per_page => -1 will load all posts from the given posts type within your slider.

To improve performance you should restrict your slider content to a special category and use the category parameter 'cat' => $id where $id is in integer value 1, 2, 3 etc.

Better still, I'd personally confine my slides to a custom post type and add 'post_type' => 'slider_posts' but this assumes you have registered a custom post type before hand.

Its important that you read http://codex.wordpress.org/Class_Reference/WP_Query which contains a great deal of information surrounding the various parameters you can use with WP_Query to achieve custom control of what data you retrieve.

  • 1
    2 very small inputs 1) wp_reset_query will also reset the postdata, no need to use both 2) To loop them again, there should be a call to $query->rewind_posts(). When inside the while loop it could go inside if($query->current_post = $query->post_count-1). but we certainly can't make it infinite Nov 2, 2012 at 11:58
  • @MridulAggarwal wp_reset_postdata shouldn't be there, I left that in. Please feel free to edit this question I will accept your revisions and thank you too! :)
    – Adam
    Nov 2, 2012 at 12:01
  • @MridulAggarwal I rejected your edit. In theory your idea is nice, but in practice this will really do an infinite loop. Meaning that it will add without an end, throw them into memory and crash. This should be done either via JS or via AJAX calling the rewind posts.
    – kaiser
    Nov 2, 2012 at 12:18
  • @kaiser Yeah i realized & then commented out the lines. It's certainly totally pointless to do such a loop Nov 2, 2012 at 12:22

If you're loading these as slides, you're probably looking at this problem wrong. You don't need to infinitely loop through the posts with PHP, you just need to present them once to the slider and makes sure it can manage repeating. What slider are you using?

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .