我认为一般的建议是WP_Query
而不是query_posts
部分原因是query\\u posts以一种简化的方式使用WP\\u query,这可能会导致问题。所以可以肯定check out the WP_Query page, specifically the Multiple Loops example: http://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
所以代码使用WP_Query
看起来像这样:
<?php
$counter = 1; //start counter
$grids = 3; //Grids per row
global $query_string; //Need this to make pagination work
/*Setting up our custom query (In here we are setting it to show 3 posts per page and eliminate all sticky posts) */
$query1 = new WP_Query( array(\'posts_per_page\'=>3, \'category_name\'=>\'Mobile\') );
if( $query1->have_posts()) : while( $query1->have_posts()) : $query1->the_post();
if( $counter == $grids ) :
$counter = 0; // Reset counter ?>
<div class="col-cat3-last">
<?php else: ?>
<div class="col-cat3">
<?php endif; ?>
<div class="entry-featured"><?php x_featured_image(); ?></div>
<div class="col-cat-pic"><?php echo get_avatar( get_the_author_meta(\'ID\'), 40); ?></div>
<div class="hero-info">
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<p class="p-meta"><?php the_author_posts_link(); ?> / <?php the_time(\'m.d.y\'); ?></p>
</div>
</div>
<?php
$counter++;
endwhile;
//Pagination can go here if you want it.
endif;
wp_reset_postdata(); // Reset post_data after each loop
?>
注意同样的
$args
可用于WP\\U查询。另请注意添加了
$query1->
循环设置中。复制粘贴此代码时,只需将$query1更改为$query2,并且很可能会更改查询参数中的category\\u名称以匹配您的类别。
我还清理了一些重复代码,因为看起来唯一的区别是在包装div类中添加了“-last”。因此,您可以使用它来代替将来需要更新的额外代码。
我还补充道wp_reset_postdata();
最后,必须安全地清除/重置post数据。
如果您有任何问题或担忧,请告诉我。