I would like to have to query loops running on my blog homepage:
- Featured posts on top displaying 3 posts from the category : "Featured"
- Underneath it just the basic loop of wordpress which display the latest (10 or other, whatever is selected within the admin) with "next" and "previous" buttons for archive posts (paged)
Important: The top part will display only 3 posts of the "Featured" category but the regular blog loop underneath will display ALL the posts including the "Featured" ones (even if it\'s a duplication - the featured posts have to appear also in the regular blog loop)
it seems prety easy but when I tried it couple of times even using rewind_posts
it broke the pagination.
This is the code I originally used:
// Featured posts section
<section class="featured">
<div class="title">
<h4>Featured</h4>
</div>
<?php query_posts(\'showposts=3&category_name=featured\'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article>
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(\'post-featured\'); ?></a>
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php excerpt(\'20\'); ?></p>
<div class="more"><a href="<?php the_permalink() ?>">Continue Reading</a></div>
</article>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</section>
// Regular normal posts section
<section class="home-posts">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post-details">
<div class="header">
<h3 class="cat">
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
</h3>
</div>
<div class="right-image">
<?php the_post_thumbnail(\'post-thumb\'); ?>
</div>
<div class="title">
<h2><a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h2>
</div>
<div class="date">
<?php include (TEMPLATEPATH . \'/inc/meta.php\' ); ?>
</div>
<div class="entry">
<p>
<?php excerpt(\'50\'); ?>
...</p>
<?php include(TEMPLATEPATH . \'/inc/share.php\'); ?>
<a href="<?php the_permalink() ?>" class="more">Continue Reading >></a> </div>
</article>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . \'/inc/nav.php\' ); ?>
<?php else : ?>
<h2> </h2>
<?php endif; ?>
</section>
hope someone can helpThanks
最合适的回答,由SO网友:Mike Madern 整理而成
您可以使用此代码从类别中获取3篇帖子Featured
$args = array(
\'category_name\' => \'featured\',
\'posts_per_page\' => 3
);
$featured_posts = new WP_Query( $args );
if ( $featured_posts->have_posts() ):
while ( $featured_posts->have_posts() ):
$featured_posts->the_post();
// Here you can use the normal loop functions like \'the_title()\' to display your
// 3 posts from the category \'Featured\'
endwhile;
endif;
在此代码之后
important 放置以下行:
wp_reset_postdata();
在循环执行单独的查询后,此函数将恢复
$post
全局到主查询中的当前帖子。
Edit
可以使用以下代码获取启用分页的循环:
$paged = ( get_query_var( \'paged\' ) )? absint( get_query_var( \'paged\' ) ): 1;
$args = array(
\'posts_per_page\' => get_option( \'posts_per_page\' ),
\'paged\' => $paged
);
$posts = new WP_Query( $args );
// Merge the two results
$posts->posts = array_merge( $posts->posts, $featured_posts->posts );
// Increase the post_count to display all the items in the merged posts array
$posts->post_count += $featured_posts->post_count;
if ( $posts->have_posts() ):
while ( $posts->have_posts() ):
$posts->the_post();
// Here you can use the normal loop functions like \'the_title()\' to display your
// posts from the category \'Featured\'
endwhile;
endif;