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') );
Nikola is correct:
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.
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
:In your template :
Posting this answer because it was the search result I ended up clicking on while searching about targeting the filter hook
the_title
while ignoring the filter effect for navigation items.I was working on a section in a theme which I wanted to add buttons to the page title within the heading one tag.
It looked similar to this:
I was then “hooking in” like this:
However, the above targets literally everything which calls
the_title
filter hook, and this includes navigation items.I changed the filter hook definition like this:
Pretty much every call to
the_title
filter passes parameter 1 as the$post->post_title
and parameter 2 as the$post->ID
. Search the WordPress core code forapply_filters( 'the_title'*
and you’ll see for yourself.So I decided to add a third parameter for situations where I want to target specific items which call
the_title
filter. This way, I can still receive the benefit of all callbacks which apply tothe_title
filter hook by default, while also having the ability to semi-uniquely target items that usethe_title
filter hook with the third parameter.It’s a simple
boolean
parameter:Label the variables however you want. This is what worked for me, and it does exactly what I need it to do. This answer may not be 100% relevant to the question asked, but this is where I arrived while searching to solve this problem. Hope this helps someone in a similar situation.
The global $dontTouch; solution didn’t work for me for some reason. So I simply removed the filter around the menu thus in header.php:
And all is well.
I think you’re looking for this: