Wordpress:在正文和评论之间插入文章列表
// 2010年02月03日 // 部落一格 // 2条留言 »
Wordpress 里显示文章的内容要用到 Loop,而我想在单篇文章的正文和评论之间加入一个显示某个类别中最新的几篇文章的块(比如推荐文章),这也要用一个Loop,那么代码应该是这样:
/* 第一个Loop:文章正文 */
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
/* 第二个Loop:显示某个类别下的最近几篇文章 *
<?php $recent = new WP_Query("cat=123&showposts=6"); while($recent->have_posts()) : $recent->the_post();?>
<?php endwhile; ?>
/* 评论模板 */
<?php comments_template(); ?>
但问题出现了,接下来的评论继承的是紧临其上的一个 Loop 也就是类别里最后的一篇文章,而不是文章正文。
我找到的解决方法是给正文的 Loop 一个ID,在执行完第二个Loop之后用这个 ID 调入第三个 Loop:
/* 第一个Loop:文章正文 加入Page ID */
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$thisPageID = get_the_ID();
$thisPageID = 'p='.$thisPageID;
?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
/* 第二个Loop:显示某个类别下的最近几篇文章 */
<?php $recent = new WP_Query("cat=123&showposts=6"); while($recent->have_posts()) : $recent->the_post();?>
<?php endwhile; ?>
/* 在评论模板前再用page ID调入第三个Loop */
<?php $recent = new wp_query($thisPageID); while($recent->have_posts()) : $recent->the_post(); ?>
/* 评论模板 */
<?php comments_template(); ?>
/* 关闭第三个 Loop */
<?php endwhile; ?>
也就是:文章正文(Loop 1)–> 推荐文章列表(Loop 2)–> 复制Loop 1的Loop 3 头部 –> 评论 –> Loop 3 尾部。
via Wordpress Support Forum by jasonmassengale








通过RSS订阅












万戈 童鞋说:
2010年02月03日19点15分
既然loop到content里了,为什么在gg reader里看不到“推荐文章”呢,应该也会随feed输出才对呀
回复
Lucifr 童鞋回复说:
@万戈, 除非改 wp-includes 下的 feed-rss2.php。像这种对主题的修改是不会影响到 rss 输出的
回复