在Wordpress中按字符限制摘录

时间:2018-06-27 12:17:23

标签: php wordpress function

对,所以似乎有人问过几次,但我似乎找不到确切的工作答案。

目前,我正在尝试限制我的WP网站上的帖子标题和帖子摘录。

我设法使用

function custom_trim_my_title( $title ) {
if ( strlen( $title ) >= 72 && ! is_singular() ) {
$title = substr( $title, 0, 72 ) . '...';
return $title;
}
return $title;
}
add_filter( 'the_title', 'custom_trim_my_title' );

这似乎很好,但是我似乎无法使其摘录起作用。

我正在使用此字数统计:

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

但是较长的单词仍然使它看起来很凌乱,因此我需要计算字符数并尝试过:

add_filter('the_excerpt','excerpt_char_limit');
function excerpt_char_limit($e){
    return substr($e,0,50);
}

但是它似乎不起作用。 任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

尝试使用

add_filter('the_excerpt','excerpt_char_limit',999);

相反。以防万一其他过滤器阻止您的过滤器。 (999表示比其他过滤器晚执行)