最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Is it possible to hide nav menu items only when they are page titles (on specific templates) but not on the

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论