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

I created a function below to hide the page title. But when I execute it, it also hides the name of the menu.

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') ); 
+10
source share
6 answers

Nicola is right:

Since the menu items also have headings, and they need to be filtered :).

To make this one call in messages, not in the menu, you can add a check to in_the_loop() - if true, you are in the message.

So, change the first line in the function to:

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

and everything should be fine.

+13
source

You can do something like this:

In 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; 
+4
source

This is 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 the_title filter inside the loop_start action, we avoid overwriting the menu title attributes.

+4
source

Conducting this answer because it was a search result on which I clicked the button while searching for the_title filter hook the_title , ignoring the filter effect for the navigation items.

I worked on a section in a topic in which I wanted to add buttons to the page title in the title tag.

It looked something like this:

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

I then "connected" as follows:

 add_filter( 'the_title', 'my_callback_function' ); 

However, above the target is literally everything that calls the_title filter, and this includes navigation elements.

I changed the definition of the filter hook as follows:

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

Almost every call to the_title filter passes parameter 1 as $post->post_title and parameter 2 as $post->ID . Find the core WordPress code for apply_filters( 'the_title'* , and you will see for yourself.

So, I decided to add a third parameter for situations where I want to target certain elements that call the the_title filter. Thus, I still benefit from all callbacks that apply to the default the_title filter binding, and also have the ability to semi-uniquely target elements that use the the_title filter the_title with the third parameter.

This is a simple boolean parameter:

 /** * @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 ); 

Label the variables as you like. This is what worked for me, and it does exactly what I need for this. This answer may not be 100% relevant to the question asked, but it was here that I came when I was looking for a solution to this problem. Hope this helps someone in a similar situation.

+1
source

Global $ dontTouch; The solution for some reason does not work for me. So I just removed the filter around the menu, this way in header.php:

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

And everything's good.

0
source

I think you are looking for this:

 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); 
0
source

All Articles