Admin tool bar in wordpress

2019-09-07 13:55发布

问题:

I am developing my own wordpress theme for very first time. I want that when admin login to WordPress, at top admin tool bar must be show on main front end of website.

I tried following things

if (is_user_logged_in()) 
 {
    show_admin_bar(true);
 }#end if

in functions.php

What I believe that I missed some thing in header.php or index.php, but I am not sure.

回答1:

The proper way to do this is with a filter in functions.php:

function my_function_admin_bar(){
    return is_user_logged_in();
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');

The admin bar is called as part of the wp_footer() function, so you need to make sure you call that function in your footer section of the template:

<?php
   wp_footer();
?>

A discussion of some specific issues that can cause this to break can be found here: http://wordpress.org/support/topic/admin-bar-not-displaying

And finally, more details on how to use the show_admin_bar() in the functions.php file can be found here: http://codex.wordpress.org/Plugin_API/Filter_Reference/show_admin_bar



标签: php wordpress