首页 › Forums › WordPress讨论区 › 综合讨论 › WordPress输出自定义长度摘要
该主题包含 0 个回复,有 1 个参与人,并且由 管理员 于 8 月, 2 周 前 最后一次更新。
正在查看帖子 1(共 1 个)
-
作者帖子
-
2012 年 11 月 14 日 pm 5:47 #922
通常我们有时需要在道面输出文章的摘要,但因为要兼顾界面的美观,所以在不同页面输出不同长度的摘要,这个时候可以自己写一个摘要输出函数即可,函数代码如下,将如下代码复制并拷贝到主题的(functions.php)文件即可。
Code: arbitrary (select)1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18./*-----------------------------------------------------------------------------------*/
/* excerpt 摘要输出更变字数函数
/*-----------------------------------------------------------------------------------*/
function excerpt($limit) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters('excerpt_length', $limit);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}使用方法,可以在index.php等面页文件中用 如:excerpt(40) 替换get_the_content(”),或the_excerpt()等文章调用函数,即输出40字摘要。
-
该主题由
管理员 于 8 月, 2 周 前 修正。
-
该主题由
-
作者帖子
正在查看帖子 1(共 1 个)
抱歉,回复评论必需登录。