让WordPress的Post和Page支持<!–more–>标签_PHP

WBOY
Release: 2016-06-01 12:20:49
Original
1012 people have browsed it

WordPress

当我们在wordpress中使用标签截断文章时,在首页、分类、标签和存档等页面的文章内容都是显示之前的文章,而在文章(Post)和页面(Page)中时,则忽略标签,显示全文。

这当然是合理的,你总不可能让访客在你的最终文章页还瞅着摘要发呆。

但对主题开发者来说,经常会使用一个Page Template来罗列一个符合查询条件的文章列表。比如下面这段代码返回分类ID为3下的所有文章。

以下为引用的内容:

<font face="NSimsun"><?php query_posts('cat=3'); while(have_posts): the_post(); ?><br><h2><?php the_title(); ?></h2> <br><?php the_content(); ?><br><?php endwhile; ?></font>

但在这份文章列表里面,the_content()函数将始终输出全文,不管你是否在文章中使用了more标签。因为WordPress只知道这是一个Page页面,要显示全文,而不会智能的根据你是否使用了query_posts()或新建了一个WP_Query对象来决定是否支持more标签。

P.S:说来惭愧,做了这么长时间的wordpress主题,我直到今天才发现这个问题。因为我个人比较喜欢“纯文字摘要列表”的方式,以往需要创建文章列表时,都是使用自动截取文章字符的方式。但正在进行的一个项目,客户要求输出html全文,然后手动插入more标签截断。

废话说了半天,其实解决方法简单的不行,请看代码(via weblogtoolscollection):

以下为引用的内容:

<font face="NSimsun"><?php global $more; $more = false; ?><br><?php the_content('Continue Reading'); ?><br><?php $more = true; ?></font>

说明: 在the_content()函数前声明一个全局变量$more并且设置为flase就可以了。如果你希望在内容显示完成后又回到原来的状态,那在后面再把$more变量设置为true。

原文地址:http://paranimage.com/the-more-tag-on-a-wordpress-page/

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!