我正在尝试替换动态页面上的 the_title 和 document_title ,例如 ?filter=most-viewed ,它有效,但唯一的问题是菜单项也被自定义标题替换。
add_filter( 'document_title', 'custom_title', 10 );
add_filter('the_title', 'custom_title', 10);
我不想为每个调用编写 2 个函数,所以我的函数是:
function custom_title( $title ) {
$filter = $_REQUEST['filter'];
if($filter=='most-viewed'){
$title = 'Most Viewed';
}
//Fix issue in the loop
if (in_the_loop() ){
global $wp_query;
return $wp_query->post->post_title;
}
return $title;
}
我尝试添加以下代码但不起作用
if (is_nav_menu() ){
global $wp_query;
return $wp_query->post->post_title;
}
如果我在
if (in_the_loop() ){
中添加所有功能,它可以解决菜单上的问题,但文档标题将不起作用。
在回调中,您可以添加
remove_action( current_action(), 'custom_title', 10 )
,限制每个操作仅运行一次。