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

Hide admin bar on all pages except homepage

programmeradmin1浏览0评论

I am trying to hide WordPress admin bar from all pages except the home page.

I added the following CODE in the theme's functions.php file, but it hides the admin bar on all pages:

if ( ! is_single() ) {
   add_filter( 'show_admin_bar', '__return_false' );
}

I want it to appear only on the homepage and to be hidden everywhere else.

I am trying to hide WordPress admin bar from all pages except the home page.

I added the following CODE in the theme's functions.php file, but it hides the admin bar on all pages:

if ( ! is_single() ) {
   add_filter( 'show_admin_bar', '__return_false' );
}

I want it to appear only on the homepage and to be hidden everywhere else.

Share Improve this question edited May 24, 2019 at 11:46 Fayaz 9,0172 gold badges33 silver badges51 bronze badges asked May 24, 2019 at 8:29 BadanBadan 2251 silver badge7 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

You should use conditional tag inside function hooked to show_admin_bar filter.

add_filter( 'show_admin_bar', 'show_adminbar_on_homepage_only', 20 );
function show_adminbar_on_homepage_only()
{
     return is_user_logged_in() && is_front_page();
}

Could be a priority issue, meaning that some plugin / theme function is overriding the admin bar behaviour. Try something like this:

if (!is_home() && !is_front_page()){
   add_filter('show_admin_bar', '__return_false', 999);
}

If this work, let's try to understand which plugins are and assign the right priority, like a lower 99 or whatever.

Here's a better way to do it that improves on the existing answers:

add_filter( 'show_admin_bar', function ( $show ) {

    if ( is_front_page() ) {
        return false;
    }

    return $show;
}, 50 );

Unlike the other code examples given, this snippet will only change the admin bar visibility if currently viewing the front page. Otherwise, it will use whatever the default is set to. This removes the need to account for different cases where the user is not logged in.

You should check if page is home page and/or front page. is_front_page() returns true when viewing the site's front page (blog posts index or a static page), is_home() returns true when viewing the blog posts index. try as below,

if (!is_home() && !is_front_page()){
   add_filter('show_admin_bar', '__return_false');
}

or

if (!is_home()){
   add_filter('show_admin_bar', '__return_false');
}
发布评论

评论列表(0)

  1. 暂无评论