现在每次写文章的时候,已经习惯了在文章和第一段或前两段之后加一个more标签,这样首页的摘要就会显示more标签之前的内容。但是一直都有一个疑问,首页index.php和日志页面single.php都是调用the_content函数,为什么一个显示摘要,一个显示全文呢?
通过阅读wordpress的源代码,发现了这样一段代码
if ( is_single() || is_page() || is_feed() ) $more = 1;
难道是通过$more变量决定的?再去了解一下the_content的源代码,果然,就是通过$more变量来决定是输出摘要还是输出全文的。为了证实这个结论,我做了一个实验:
修改index.php文件,在the_content之前强行把$more的值修改为1
$more = 1; the_content();
结果之前一直显示摘要的首页,现在显示的是全文内容。同样,再去修改singel.php文件,在the_content之前强行把$more的值修改为0
$more = 0; the_content();
结果日志页面显示的是摘要,而不是全文。
通过以上实验可以看出,the_content函数是输出全文还是输出摘要,是这个全局变量$more决定的。理解了这一点,我们就可以更灵活的运用the_content函数。下面通过一个实例来结束本文。
首页的第一篇文章显示全文,其它文章显示摘要:
<?php if(have_posts() ){
$more = -1;
while(have_posts()){
the_post();
// ......
if($more == -1){
$more = 1;
the_content();
$more = 0;
} else {
the_content('继续阅读...');
}
// ......
}//end while(have_posts)
}//end if(have_posts())
else{
echo '没有找到相关文章';
}//end else
?>
万戈
2010-01-14 星期四 11:52
卜卜口の
2010-07-01 星期四 12:34
皇家元林
2010-09-09 星期四 17:57
一米
2010-11-12 星期五 18:57
mymoe
2011-01-08 星期六 13:04
小谈博客
2011-10-12 星期三 19:31