Wordpress loop repeat itself
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 t开发者_JS百科wo loops... easy answer would be to have 2 loops, each with 10 posts, making it equal 20.
Thx
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');
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.
there is a setting in back end which you can change the maximum number of posts on the homepage - look under settings->reading
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
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
精彩评论