tags:

views:

64

answers:

5

Hey there, 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

A: 

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

matpol
Ty for your answer, tho please read the question carefully:)
webmasters
A: 

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
Fernando
This looks interesting.. ty fernando... thinking how to make it loop again
webmasters
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
webmasters
+1  A: 

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.

songdogtech
Even though I love coding just for fun, this still makes more sense to me.
Fernando
A: 

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

Ramuns Usovs
+1  A: 

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');
John P Bloch
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
webmasters
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.
John P Bloch
John, thank you! Really thank u!
webmasters
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.
John P Bloch
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
webmasters
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.
John P Bloch