开发者

why is the <?php wp_head ?> tag creating a top margin at the top of my theme header

Hey people, I've been coming here for a while but just decided to 开发者_StackOverflow中文版join.

I'm new to php and I'm trying to make a website with WordPress as the CMS.

Anyway, I'm basically making my own theme because I don't want my website to look like a blog, and it's going pretty smoothly so far but theres this huge top margin gap in the browser even when I set margins to 0px.

I tried trial and error and found out that it's being caused by: <?php wp_head(); ?>


It's two different things.

1. wp_head() function

wp_head() is a template tag to print whatever plugin / theme specific function used by wordpress action. Read codex for more detail about it.

2. The admin bar

The top margin, is generated by wordpress's admin bar.

To fix this for logged in users you can do a couple of things:

Disable admin bar from the admin:

  1. Go to admin panel
  2. Users >> User Profile
  3. Uncheck 'when viewing site' on 'Show Admin Bar'

Remove the admin bar from your theme entirely:

  1. Open your functions.php
  2. Add this to it:

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

Remove only the code which creates the css:

  1. Open your functions.php
  2. Add this to it:

    function my_filter_head() { remove_action('wp_head', '_admin_bar_bump_cb'); }
    add_action('get_header', 'my_filter_head');
    

Note: Extensive updates are from @hitautodestruct


you neeed to call wp_footer() in your footer

just insert this line <?php wp_footer();?> into your footer.php file


If you have updated your wordpress install to the latest version.. there seems to be some bug with the admin bar...

were it would produce an inline stylesheet appended to the top of your pages.. causing the margin-top:28px

see here

1 recomendation is to put a new function into your functions.php file located in your theme folder.. this will completly remove the bar, so no users of your theme will have any of the same issues!

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

Not sure if this will help.. but worth a shot.. also turning off the admin bar while viewing the front end of the site under your profile page..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜