2 loops in blog homepage

时间:2013-03-18 作者:gil hamer

I would like to have to query loops running on my blog homepage:

  1. Featured posts on top displaying 3 posts from the category : "Featured"
  2. 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>&nbsp;</h2>
  <?php endif; ?>
</section>

hope someone can helpThanks

1 个回复
最合适的回答,由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;

结束

相关推荐

Recent posts on homepage

我已经做了一个页面,你们可以从我的帖子中找到所有的挑逗者(用阅读更多标签制作的挑逗者)。(通过单击其中一个,您可以“打开”整篇文章,包括照片,…。这很好。在我的主页上,我想要一个简短的欢迎文本,并在侧栏中显示我最近的3篇帖子。我首先尝试使用以下代码在我的主页上获取最近的帖子<?php function recentPosts() { $rPosts = new WP_Query(); $rPosts->query(\'showposts=3\');&#x