使用 str_replace 改变 the_title()

Using str_replace to alter the_title()

我有这个代码片段:

$nieuwetitel = the_title();
echo str_replace('Bijenkorf', 'test', $nieuwetitel);

the_title() 输出以下内容:Bijenkorf – Acne Studios Adriana €360

使用上述代码,我想将 'Bijenkorf' 更改为 'test',但这似乎不起作用。

最后,我想创建一个列表,列出要从 the_title() 中排除的内容(Bijenkorf 是其中之一)。

您可以为此使用 the_title 过滤器。

这是代码。

function wh_alterProductTitle($title, $id = NULL)
{
    //for only changing product title
    if ('product' == get_post_type($id))
    {
        //for single
        $title = str_replace('Bijenkorf', 'test', $title);
        //for multiple
        //$title = str_replace(['find1', 'find2'], 'test', $title);
    }
    return $title;
}

add_filter('the_title', 'wh_alterProductTitle', 10, 2);

希望对您有所帮助!