Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the_title() filter is also applied in menu title?

I have created below function to hide page title. But when I execute this code, it also hides the menu name.

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') );
like image 805
softsdev Avatar asked Nov 19 '12 15:11

softsdev


2 Answers

It's a bit of a hack but you can solve this by adding your action to 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' );

By embedding the_title filter inside of a loop_start action, we avoid overwriting the menu title attributes.

like image 69
Imperative Avatar answered Oct 22 '22 10:10

Imperative


Nikola is correct:

Because menu items also have titles and they need to be filtered :).

To make this only call in the posts, and not in menus, you can add a check for in_the_loop() - if it is true, you're in a post.

So change the first line in the function to:

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

and all should be well.

like image 15
Paul Gregory Avatar answered Oct 22 '22 08:10

Paul Gregory