WordPress:在正文和评论之间插入文章列表

2010年02月03日

分类: 部落一格

loop_before_comment

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

分享或收藏本文:
  • Twitter
  • Buzz
  • Google Bookmarks
  • del.icio.us
  • 收藏到鲜果
  • QQ书签
  • 豆瓣九点
  • 豆瓣
  • 新浪微博
  • 人人网
  • 开心网
  • DigLog
  • 百度搜藏
  • Live
  • Yahoo! Bookmarks
  • Tumblr
  • Facebook
  • Add to favorites
  • email

{ 2 条评论… 阅读评论或参加讨论 }

1 万戈 2010年02月03日 at 19:15

既然loop到content里了,为什么在gg reader里看不到“推荐文章”呢,应该也会随feed输出才对呀

Reply

2 Lucifr 2010年02月03日 at 19:57

@万戈, 除非改 wp-includes 下的 feed-rss2.php。像这种对主题的修改是不会影响到 rss 输出的

Reply

发表评论

前一篇文章:

后一篇文章:

唾沫新星

唾沫新星

  • Lifestream