wordpress - 缩短我的网格 - 存档 - 页面的帖子标题网址
问题描述
正如标题所说,我需要缩短我的网格 - 存档 - 页面的帖子标题。例如,我的页面标题是“清洁汽车的 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' );
所以希望有人能够帮助我。提前致谢。
解决方案
试试这个:
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' );
推荐阅读
- android - Android - Tab and mobile compatible codes (supported screens
- ios - Xcode PDFKit form automatic calculation
- dns - 代客:找不到 DNS 地址
- arangodb - Push/Pull Value(s) to Array in ArangoJS
- typescript - Simplify async-await with array iteration in Typescript
- python-3.x - parse xml with lxml
- c - Crashing issue, might be due to a for loop
- android - 侦听器中的 PrintWriter 崩溃应用程序
- amazon-web-services - How do I write unit test aws-sdk-go-v2 dynamodb implementation
- wpf - 如何将 XAML 主窗口中的网格用于 WPF 中的另一个类?