首页 > 解决方案 > 如何在一个单词的索引之后获得标题的一部分 - 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;

标签: phpwordpress

解决方案


最简单但最不优雅:

str_replace('Words from a farm in ', '', get_the_title())

推荐阅读