首页 开发指南

我们在用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。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

会员评论:(0)

声明:本站所有主题/文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除,谢谢!
©www.sbmzenith.com 2013-2022 All Rights Reserved.
豫ICP备15009393号