
我们在用wordpress做企业主题的时候,经常会碰到这个问题:在产品列表页面,想调出来一个新闻列表怎么办,这个时候,我们一个页面就会出现2个甚至更多个主循环。
?<h3>NEWS & EVENTS</h3>
<?PHP
query_posts("cat=12&orderby=date&order=ASC&posts_per_page=4");
if (have_posts()) :
while (have_posts()) : the_post();
?>
<span><b><?php the_time('Y/n/j'); ?></b> - <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></span>
<?php endwhile; ?>
<?php endif;
??>??
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?>
<li>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php
hxjq_the_thumbnail();
?>
<p><?php
$content=strip_tags(get_the_content());
echo mb_strimwidth($content,0,100,"...");
?></p>
</li>
<?php endwhile; ?>
<?php endif; ?>
这个时候,由于前一个query_post指定了一个cat,后个就也会输出这个ID的内容。这是因为wp_query()或query_posts()打乱了wordpress常规的loop循环,wp_query()或query_posts()所使用的参数是全局的,应该再用wp_reset_query()重置Query循环。
当你使用wp_query()或query_posts()时,应该再用wp_reset_query()重置Query循环
 <h3>NEWS & EVENTS</h3>
<?PHP
query_posts("cat=12&orderby=date&order=ASC&posts_per_page=4");
if (have_posts()) :
while (have_posts()) : the_post();
?>
<span><b><?php the_time('Y/n/j'); ?></b> - <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></span>
<?php endwhile; ?>
<?php endif;
wp_reset_query();
 ?>
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?>
<li>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php
hxjq_the_thumbnail();
?>
<p><?php
$content=strip_tags(get_the_content());
echo mb_strimwidth($content,0,100,"...");
?></p>
</li>
<?php endwhile; ?>
<?php endif; ?>
该代码的作用是显示新的1篇文章相关信息,并在末尾用wp_reset_query()重置query。
上一篇: wordpress 导航特效
游客回答:(0)