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

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


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.


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.


You can do something like that :

In your function.php :

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

In your template :

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