WordPress的定制摘录长度不工作
问题描述:
我有这样的代码在我的functions.phpWordPress的定制摘录长度不工作
function custom_excerpt_length() {
return 15;
}
add_filter('excerpt_length', 'custom_excerpt_length');
,但它不工作,因为它给了我充分的文本,而不是我指定的15个字。我在网站上设置的网格并不正确,因为这个。
在此先感谢
答
也可使用此代码对多个类型越来越摘录
的function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
然后在你的项目您刚使用的板代码..
<?php echo excerpt(25); ?>
答
添加第三个参数,你也没有提到函数参数将工作
function custom_excerpt_length($length) {
return 15;
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);
+0
什么是第三个参数? – scottiescotsman
+0
您的功能优先级正确优先。你忘了添加像'$ length'这样的函数参数 –
正确添加第三个参数作为优先级,并且您没有传递参数作为功能,那么请检查您的输出。 –