首页 > 解决方案 > 缩短我的网格 - 存档 - 页面的帖子标题网址

问题描述

正如标题所说,我需要缩短我的网格 - 存档 - 页面的帖子标题。例如,我的页面标题是“清洁汽车的 20 种方法。2018 年最佳提示和技巧。 ”但在存档页面上,我只想显示“清洁汽车的 20 种方法” 。

我发现一些代码在一定数量的字母后停止,但如果出现某个单词,我想停止。以“最佳”为例。也许有人可以帮助我。

这是我到目前为止尝试的代码。

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

所以希望有人能够帮助我。提前致谢。

标签: wordpressgridtitlearchivestrlen

解决方案


试试这个:

function custom_trim_my_title( $title ) {
    if ( !is_singular() ) {
        //if you pass $title as "this is a stop", it will return "this is a"
        $new_title = strstr($title, 'stop', true);
        if($new_title) {
            return $new_title;
        }
    }
    return $title;
}
add_filter( 'the_title', 'custom_trim_my_title' );

推荐阅读