我创建了下面的函数来隐藏页面标题。但是当我执行这段代码时,它也隐藏了菜单名称。
function wsits_post_page_title( $title ) {
if( is_admin())
return $title;
$selected_type = get_option('wsits_page_show_hide');
if(!is_array($selected_type)) return $title;
if ( ( in_array(get_post_type(), $selected_type ) ) && get_option('wsits_page_show_hide') )
{
$title = '';
}
return $title;
}
add_filter( 'the_title', array($this, 'wsits_post_page_title') );
最佳答案
尼古拉是正确的:
Because menu items also have titles and they need to be filtered :).
要使此调用仅在帖子中而不是在菜单中调用,您可以添加对 in_the_loop()
的检查 - 如果为真,则您在帖子中。
所以把函数的第一行改成:
if( is_admin() || !in_the_loop() )
一切都会好起来的。
关于php - 为什么 the_title() 过滤器也应用于菜单标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13456521/