php - 如何在一个单词的索引之后获得标题的一部分 - wordpress
问题描述
我有一个生成帖子列表的简码。每个都显示带有 的标题get_the_title()
。
但是,我需要 indexOf 标题以仅显示其中的一部分并删除常量部分。我不能只更新帖子的标题,尽管这会更容易,因为我在其他地方使用标题。
例子:
职位名称列表:
纽约州罗切斯特农场的话
来自俄亥俄州斯普林菲尔德的一个农场的话
宾夕法尼亚州西约克的一个农场的话
佛罗里达州霍姆斯特德农场的话
然后在functions.php文件中,我有一个这样的标题循环
$string .= '<h4 class="uk-card-title">' . get_the_title() . '</h4>';
我需要仅在循环中删除“农场中的单词”的东西
代码
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<div class="uk-container-expand">';
$string .= '<div class="postsbycategory widget_recent_entries posts" uk-grid="masonry: true">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$text = str_replace('Words from a farm in ', '', get_the_title());
$string .= '<div class="uk-width-1-3@m uk-width-1-1@s">';
$string .= '<div class="uk-card uk-card-secondary uk-card-secondary-alt post">';
$string .= '<div class="uk-card-header">';
$string .= '<h4 class="uk-card-title">';
$string .= str_replace('Words from a farm in ', '', get_the_title());
$string .= $text;
$string .= '</h4>';
$string .= '</div>';
$string .= '<div class="uk-card-body">';
$string .= '<p>' . get_the_content() . '</p>';
$string .= '</div>';
$string .= '</div>';
$string .= '</div>';
}
} else {
// no posts found
}
return $string;
解决方案
最简单但最不优雅:
str_replace('Words from a farm in ', '', get_the_title())
推荐阅读
- javascript - 如何将传单地图中的响应式图像与 css 对齐
- jquery - 如何使用 Coin Slider 获取图像以形成幻灯片?
- python - Pandas:在遍历行时有条件地将行插入 DataFrame
- google-maps - Polymer 3 - 谷歌地图文件丢失
- html - 我想制作一个同时返回文本和图像的服务器,只使用python套接字而不使用simplehttpserver之类的模块
- javascript - Vue中如何动态生成div?
- java - 使用 Retrofit 时,由于输入结束,没有要映射的内容
- java - Android 媒体提供程序没有获取最近添加的图像
- ios - 如何使用 IBInspectable 来控制子视图的属性?
- javascript - jQuery ajax 请求在控制台中成功,但未调用回调