2017-03-07 110 views
0

我有这样的代码片断:使用str_replace函数来改变the_title()

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

the_title()输出以下:比热卡夫 - 痤疮工作室阿德里安娜€360

使用上面提到的代码,我想将'Bijenkorf'改为'测试',但这似乎并不奏效。

最后我想创建一个排除在the_title()(Bijenkorf是其中之一)的事情列表。

回答

1

您可以使用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); 

希望这会有所帮助!

+0

这个伎俩!非常感谢 :) – Matthias