1
votes

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? - gabe3886
yes, on my front page, show the first 10 posts then duplicate them - webmasters
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?) - markratledge
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:) - webmasters
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. - xintron

5 Answers

1
votes

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');
1
votes

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.

0
votes

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

0
votes

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
0
votes

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