1

Lets say i have a blog with 10 posts. I want to display 20 posts on my frontpage.

How can i make the wordpress loop repeat itself till it reaches 20?

if ( have_posts() ) : while ( have_posts() ) : the_post(); endwhile; endif;

Btw... i do not want two loops... easy answer would be to have 2 loops, each with 10 posts, making it equal 20.

Thx

5
  • If your blog only had 10 posts, where would the other 10 come from? Would you duplicate the posts to make it 20? Commented Aug 16, 2010 at 13:25
  • yes, on my front page, show the first 10 posts then duplicate them Commented Aug 16, 2010 at 13:30
  • Exactly. If you have less then 20 posts, are you looking for conditional logic that asks if there are less than 20 posts, reprint posts until 20 are displayed? (And what's the logic in that?) Commented Aug 16, 2010 at 13:31
  • About the logic, I need it for a specific project, a site displaying thumbs, like a tgp style. Since i'm building alot of sites, don't have the time to write 20, 30 posts at once... so the fix is temporary... each post has a thumb displayed... if i only have 5 posts, the page would look strange.... but if i repeat them till 20, the page looks good untill i actually get the chance to write the 20:) Commented Aug 16, 2010 at 13:35
  • 2
    That makes no sense. Why not use a Lorem Ipsum placeholder for development and then prepare enough posts before publishing the site? Duplicate content would (probably) make it look weird, even a temporary Lorem Ipsum would do better. Commented Aug 16, 2010 at 13:44

5 Answers 5

1

Drop this in your theme's functions.php file:

function my_awesome_post_booster(){
  if(!is_home())
    return;
  global $wp_query;
  if( $wp_query->post_count < 20 ){
    $how_many = 20 - $wp_query->post_count;
    $newposts = get_posts('numberposts='.$how_many);
    $wp_query->posts = array_merge( $wp_query->posts, $newposts );
    $wp_query->post_count += count($newposts);
    my_awesome_post_booster();
  }
}

add_action('template_redirect', 'my_awesome_post_booster');
Sign up to request clarification or add additional context in comments.

5 Comments

this is one cool code john. It displays the first 10, then the first 5, then for the last 5 it doesn't display any links... why? Btw,could u explain this really cool code;)? a lil
I fixed the 'only shows 15' problem. What it does is this: First, it looks to see how many posts are going to be displayed by default. If it's fewer than 20, it grabs the difference (in this case, it should grab 10 posts) and appends them to the current query. Then it recurses through itself to see if it's still under 20 posts. If so, it continues to run until it hits at least 20 posts.
No problem. Also, it should be noted that this will only work on the 'home' page. Just remove the return to make it work everywhere. I should warn you, though, that will apply to EVERYTHING, including pages.
Hey john, one more thing.. for example I only have two posts... The coded show's the two posts once more, then it stops to run. So i only get 4 posts... instead of the code looping to repeat my 2 posts until they reach 20.. any idea why? Thank you
Yeah, sloppy on my part. I was adding the difference between 20 and the original number of posts instead of the number of posts returned, so it thought there were twenty posts. Fixed that now. Just a warning, though: with only two posts, you're going to be querying the database 10 times per request, so you might want to reconsider using this with so few posts.
1

Use placeholder Lorem Ipsum text http://www.lipsum.com to make enough posts and use the same thumb for them. Makes more sense than writing a new loop (though it would be easy) and placing/replacing that in themes.

And if you're concerned about SEO, those concerns are entirely misplaced. Block your development site from search bots, as you don't want an site indexed with multiple posts and/or Lorem Ipsum text. Once the site is live on a domain, then do a sitemap and let the bots in.

1 Comment

Even though I love coding just for fun, this still makes more sense to me.
0

there is a setting in back end which you can change the maximum number of posts on the homepage - look under settings->reading

Comments

0

This link on WordPress Codex will help you out: The Loop - Multiple loops in Action

You can query posts with WP_Query, if you don't have at least 10, then you can loop the results again.

<?php 
$my_query = new WP_Query('category_name=whatever');
$count = 0;
while ($my_query->have_posts()) { 
    $my_query->the_post();
    $count++;
}
if (count < 10) {
    //loop again or something

2 Comments

This looks interesting.. ty fernando... thinking how to make it loop again
The idea is, for seo purposes... no sense in writing 20 posts at once.. but to write them over a period of a month even
0

How about something like this:

$count = 0;
while ( $count < 20 ) { 
    if ( !have_posts() ) { 
        rewind_posts();
    }
    the_post();
    $count++;
}

This of course assumes that the query does have at least one post

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.