移除管理栏的 28 像素下推

Avatar of Chris Coyier
Chris Coyier

适用于你的 functions.php 文件

  add_action('get_header', 'my_filter_head');

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

默认情况下,如果你以登录的 WordPress 用户身份看到管理栏,以下 CSS 代码将在你的头部输出(在 wp_head() 函数中输出)。

<style type="text/css" media="screen">
	html { margin-top: 28px !important; }
	* html body { margin-top: 28px !important; }
</style>

这通常是一件好事,因为它不会用其固定定位覆盖你网站的部分内容。但是,如果你对某些元素使用绝对定位,它可能会很奇怪。因为它们的位置会根据管理栏是否存在而不同。使用上面的代码移除 bump CSS 将使管理栏覆盖你网站的顶部,但至少定位将保持一致。