我想创建一个简单的函数来调用登录用户的用户名以及单词“Wishlist”。
这似乎是工作,但它也改变了菜单项的标题!
function customize_wishlist_title( $title ){
$current_user = wp_get_current_user();
$loggeduser = $current_user->user_login;
if ( is_page( 258 ) ) {
$title = \'<span class="username">\'.$loggeduser.\'\\\'s\'.\'</span>\'.\' \'.\'<span>Wishlist</span>\';
}
return $title;
}
add_filter(\'the_title\',\'customize_wishlist_title\', 10, 2);
最合适的回答,由SO网友:Max Yudin 整理而成
使用in_the_loop()
条件标记,以确保您正在修改循环中的标题,并防止挂钩在其他地方更改标题:
function customize_wishlist_title( $title ){
$current_user = wp_get_current_user();
$loggeduser = $current_user->user_login;
// give attention to the following condition:
if ( is_page( 258 ) && in_the_loop() ) {
$title = \'<span class="username">\'.$loggeduser.\'\\\'s\'.\'</span>\'.\' \'.\'<span>Wishlist</span>\';
}
return $title;
}
add_filter(\'the_title\',\'customize_wishlist_title\', 10, 2);
请参见
in_the_loop()
参考