I want to hide the page titles that use a custom page template. So I've written code that filters out the page title when the custom page template is enabled. However, that resulted in the nav menu items being filtered too. So I added more code to remove the filter if it is a menu item and add it back after. But now, the titles are being filtered in the Dashboard as shown in the picture. How do I prevent this?
The code that I added is as follows:
function suppressed_title($title,$id) {
if(get_page_template_slug()=='custom-template.php' && get_post_type($id)=='page') {
$title='';
}
return $title; }
add_filter('the_title', 'suppressed_title',10,2);
function remove_title_filter($nav_menu, $args) {
remove_filter('the_title', 'suppressed_title',10,2);
return $nav_menu;
}
add_filter('pre_wp_nav_menu', 'remove_title_filter',10,2);
function add_title_filter($items, $args) {
//add the filter after filtering out the menu items
add_filter('the_title', 'suppressed_title',10,2);
return $items;
}
add_filter('wp_nav_menu_items', 'add_title_filter',10,2);
I want to hide the page titles that use a custom page template. So I've written code that filters out the page title when the custom page template is enabled. However, that resulted in the nav menu items being filtered too. So I added more code to remove the filter if it is a menu item and add it back after. But now, the titles are being filtered in the Dashboard as shown in the picture. How do I prevent this?
The code that I added is as follows:
function suppressed_title($title,$id) {
if(get_page_template_slug()=='custom-template.php' && get_post_type($id)=='page') {
$title='';
}
return $title; }
add_filter('the_title', 'suppressed_title',10,2);
function remove_title_filter($nav_menu, $args) {
remove_filter('the_title', 'suppressed_title',10,2);
return $nav_menu;
}
add_filter('pre_wp_nav_menu', 'remove_title_filter',10,2);
function add_title_filter($items, $args) {
//add the filter after filtering out the menu items
add_filter('the_title', 'suppressed_title',10,2);
return $items;
}
add_filter('wp_nav_menu_items', 'add_title_filter',10,2);
Share
Improve this question
asked Nov 8, 2021 at 13:56
SuPalleSuPalle
31 bronze badge
1 Answer
Reset to default 0You can use is_admin()
to check if the request is done being inside /wp-admin.
function suppressed_title($title, $id) {
if (is_admin()) {
return $title;
}
if (get_page_template_slug() === 'custom-template.php' && get_post_type($id) === 'page') {
$title = '';
}
return $title;
}
add_filter('the_title', 'suppressed_title', 10, 2);
I changed your code to use triple equals ===
over ==
as this is usually considered best practice. (See this StackOverflow answer for more info.)
I want to hide the page titles that use a custom page template
Technically, you're not hiding the title right now, but displaying an empty title. This might have effects on the styling anyway. In my opinion a cleaner solution would be not to display the title at all at these pages. But this is highly dependent on which theme you are using.