Php 为什么菜单标题中也应用了_title()过滤器?

Php 为什么菜单标题中也应用了_title()过滤器?,php,wordpress,filter,themes,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

我创建了下面的函数来隐藏页面标题。但当我执行它时,它也会隐藏菜单名

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') );
尼古拉是正确的:


因为菜单项也有标题,需要对其进行筛选:)

要仅在post中进行此调用,而不是在菜单中进行此调用,可以在循环()中添加对
的检查-如果为真,则表示您在post中

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

if(是_admin()| |!在_循环()中)


一切都会好起来。

你可以这样做:

function.php
中:

add_filter( 'the_title', 'ze_title');
function ze_title($a) {
    global $dontTouch;
    if(!$dontTouch && !is_admin())
        $a = someChange($a);
    return $a;
}
在模板中:

$dontTouch = 1;
wp_nav_menu( array('menu' => 'MyMenu') );
$dontTouch = 0;

这有点像黑客,但你可以通过将你的动作添加到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' );

通过在循环开始操作中嵌入标题过滤器,我们可以避免覆盖菜单标题属性。

发布此答案,因为这是我在搜索过滤器钩子时单击的搜索结果
标题
,而忽略导航项的过滤效果

我正在处理一个主题中的一个部分,我想在heading one标记中为页面标题添加按钮

看起来与此类似:

<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>
add_filter( 'the_title', 'my_callback_function' );
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>
但是,上面的目标实际上是调用
过滤器钩子的所有内容,其中包括导航项

我更改了过滤器挂钩定义,如下所示:

<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>
add_filter( 'the_title', 'my_callback_function' );
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>
根据需要标记变量。这正是我所需要的,它正是我所需要的。这个答案可能与所问的问题没有100%的相关性,但这正是我在寻求解决这个问题时到达的地方。希望这能帮助处于类似情况的人。

全球$dontTouch;由于某种原因,这个解决方案对我不起作用。因此,我只是在header.php中删除了菜单周围的过滤器:


一切都很好

我想你在找这个:

function change_title($title) {
    if( in_the_loop() && !is_archive() ) { // This will skip the menu items and the archive titles
        return $new_title;              
    }    
    return $title;    
}
add_filter('the_title', array($this, 'change_title'), 10, 2); 

因为菜单项也有标题,需要对其进行筛选:)。如果这是您的主题,您可以在显示菜单之前删除过滤器,然后再次添加。或者,你可以做相反的事情,只在你需要的时候添加过滤器。在显示菜单标题后,有人会如何删除过滤器并重新添加?@Paul这不适用于包含自定义菜单的小部件。