为什么the_title()过滤器也适用于菜单标题?

sof*_*dev 11 php wordpress themes filter

我创建了以下功能来隐藏页面标题.但是当我执行它时,它也隐藏了菜单名称.任何人都可以帮助,我们将非常感谢.

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') );
Run Code Online (Sandbox Code Playgroud)

Pau*_*ory 14

尼古拉是对的:

因为菜单项也有标题,需要进行过滤:).

要在帖子中进行此调用,而不是在菜单中进行调用,您可以添加一个检查in_the_loop() - 如果是,则您在帖子中.

因此,将函数中的第一行更改为:

if( is_admin() || !in_the_loop() )

一切都应该好.


Imp*_*ive 5

这有点hack,但是您可以通过将操作添加到loop_start来解决此问题。

function make_custom_title( $title, $id ) {
    // Your Code Here
}

function set_custom_title() {
   add_filter( 'the_title', 'make_custom_title', 10, 2 );
}

add_action( 'loop_start', 'set_custom_title' );
Run Code Online (Sandbox Code Playgroud)

通过将the_title过滤器嵌入loop_start动作中,我们避免覆盖菜单标题属性。