admin管理员组文章数量:1429577
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 badges4 Answers
Reset to default 1You 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');
}
本文标签: Hide admin bar on all pages except homepage
版权声明:本文标题:Hide admin bar on all pages except homepage 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745454195a2659025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论