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

标签 php wordpress filter themes

我创建了下面的函数来隐藏页面标题。但是当我执行这段代码时,它也隐藏了菜单名称。

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/

相关文章:

php - yii中的基本隐藏字段

android - 我应该如何用字符串过滤相应的复选框

wordpress - 为什么网站名称首先显示在 Google 搜索结果中的标题中

image-processing - 如何获取 CNN 中的输入和输出 channel ?

ruby - Ruby 中的音频均衡器

PHP:两个日期之间的差异对于三月来说不正确

php - 如何从数据库中列出用户类型并允许从管理面板中进行选择?

php - 给出警告 : mysql_query() expects parameter 2 to be resource, 对象

javascript - 无法隔离正确的 clientHeight 值

css - 在 Wordpress 中使用 html 和 sass