如何只更改wordpress帖子标题而不更改菜单项。
add_filter(\'the_title\', \'wordpress_title\');
function wordpress_title(){
return \'New title\';
}

如何只更改wordpress帖子标题而不更改菜单项。
add_filter(\'the_title\', \'wordpress_title\');
function wordpress_title(){
return \'New title\';
}
add_filter(\'the_title\', \'wordpress_title\');
function wordpress_title($title){
//Return new title if called inside loop
if ( in_the_loop() )
return \'New title\';
//Else return regular
return $title;
}
你试过in_the_loop()
条件检查,仅当在循环内调用时才返回新标题。这意味着导航菜单不会受到影响。如果您使用的是自定义导航菜单,则可以完全不用代码即可完成此操作。转到Appearance
-> Menus
并更改您想要改变的每个菜单项的“导航标签”。
<?php add_filter(\'the_title\', function($title) { return \'<b>\'. $title. \'</b>\';}) ?>
您需要一组正确的条件:
在循环内部,帖子的ID与URL中的ID相匹配(有点复杂)
add_filter( \'the_title\', \'change_my_title\');
function change_my_title ($title) {
if ( in_the_loop() && get_the_ID() === url_to_postid(full_url($_SERVER))) {
$title = $title . " added by plugin";
}
return $title;
}
// Function found here: http://stackoverflow.com/a/8891890/358906
function full_url($s) {
$ssl = (!empty($s[\'HTTPS\']) && $s[\'HTTPS\'] == \'on\') ? true:false;
$sp = strtolower($s[\'SERVER_PROTOCOL\']);
$protocol = substr($sp, 0, strpos($sp, \'/\')) . (($ssl) ? \'s\' : \'\');
$port = $s[\'SERVER_PORT\'];
$port = ((!$ssl && $port==\'80\') || ($ssl && $port==\'443\')) ? \'\' : \':\'.$port;
$host = isset($s[\'HTTP_X_FORWARDED_HOST\']) ? $s[\'HTTP_X_FORWARDED_HOST\'] : isset($s[\'HTTP_HOST\']) ? $s[\'HTTP_HOST\'] : $s[\'SERVER_NAME\'];
return $protocol . \'://\' . $host . $port . $s[\'REQUEST_URI\'];
}
我已经将下面的代码添加到一个活动插件中,但它对我的帖子没有任何影响。add_filter( ‘the_title’, ‘myfunction’); function myfunction($title) { return \"Why won\'t this work?\" . $title; } 我错过了什么?帖子模板肯定使用了\\u title(),主题是正常的(wp\\u head(),等等),插件中的函数周围没有使其无法运行的条件。我还尝试在add\\u