12

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

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') );
4

6 回答 6

14

尼古拉是正确的:

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

要使此操作仅在帖子中调用,而不是在菜单中,您可以添加检查in_the_loop() - 如果它是真的,您在帖子中。

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

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

一切都应该很好。

于 2012-11-19T15:43:51.067 回答
6

这有点小技巧,但您可以通过将操作添加到 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' );

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

于 2014-10-16T08:52:23.977 回答
4

你可以这样做:

在你的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;
于 2012-12-11T22:09:34.627 回答
1

发布此答案是因为它是我在搜索有关定位过滤器挂钩时最终单击的搜索结果,the_title而忽略了导航项的过滤器效果。

我正在研究一个主题中的一个部分,我想在标题一个标签内向页面标题添加按钮。

它看起来类似于:

<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>

然后我像这样“上钩”:

add_filter( 'the_title', 'my_callback_function' );

但是,上面的目标实际上是所有调用the_title过滤器钩子的东西,这包括导航项。

我像这样更改了过滤器挂钩定义:

<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>

几乎每次对the_titlefilter 的调用都将参数 1 作为 传递,将$post->post_title参数 2 作为$post->ID. 搜索 WordPress 核心代码apply_filters( 'the_title'*,您将亲眼看到。

所以我决定为我想要针对调用the_title过滤器的特定项目的情况添加第三个参数。这样,我仍然可以从the_title默认情况下适用于过滤器挂钩的所有回调中受益,同时还能够半唯一地定位使用the_title带有第三个参数的过滤器挂钩的项目。

这是一个简单的boolean参数:

/**
 * @param String $title
 * @param Int $object_id
 * @param bool $theme
 *
 * @return mixed
 */
function filter_the_title( String $title = null, Int $object_id = null, Bool $theme = false ) {

    if( ! $object_id ){
        return $title;
    }

    if( ! $theme ){
        return $title;
    }

    // your code here...

    return $title;

}

add_filter( 'the_title', 'filter_the_title', 10, 3 );

根据需要标记变量。这对我有用,它完全符合我的需要。这个答案可能与所提出的问题不是 100% 相关,但这是我在寻找解决这个问题时到达的地方。希望这对处于类似情况的人有所帮助。

于 2017-01-23T23:01:47.977 回答
0

我想你正在寻找这个:

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); 
于 2019-01-25T18:23:21.497 回答
0

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

remove_filter( 'the_title', 'change_title' );
get_template_part( 'template-parts/navigation/navigation', 'top' ); 
add_filter( 'the_title', 'change_title' );

一切都很好。

于 2018-11-20T15:34:44.393 回答