当我自定义一个WP主题时,我应该把导航栏放在“<body>”标签之前还是之后?


Should I place my navbar before or after the `<body>` tag when customizing a WP theme?

我通过制作子主题自定义一个WP主题。我把导航栏从引导到子主题目录的header.php文件。但是,我不确定在哪里放置导航条代码。我可以成功地将它放在之前和之后的<body>标签(例如,无论我选择哪一个,导航条都显示得很好),但我想打电话给一位有经验的朋友,询问哪种做法更好。

原始的,未修改的header.php代码

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width" />
    <title><?php wp_title( ' | ', true, 'right' ); ?></title>
    <link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_uri(); ?>" />
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?> >
    <div id="wrapper" class="hfeed">
        <header id="header" role="banner">
        <section id="branding">
            <div id="site-title"><?php if ( ! is_singular() ) { echo '<h1>'; } ?><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php esc_attr_e( get_bloginfo( 'name' ), 'blankslate' ); ?>" rel="home"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></a><?php if ( ! is_singular() ) { echo '</h1>'; } ?></div>
            <div id="site-description"><?php bloginfo( 'description' ); ?></div>
        </section>
        <nav id="menu" role="navigation">
            <div id="search">
                <?php get_search_form(); ?>
            </div>
            <?php wp_nav_menu( array( 'theme_location' => 'main-menu' ) ); ?>
        </nav>
        </header>
        <div id="container">

我的导航条代码将替换上面代码中的<nav>部分,如下所示

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width, initial scale=1" />
    <title><?php wp_title( ' | ', true, 'right' ); ?></title>
    <link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_uri(); ?>" />
    <?php wp_head(); ?>
</head>
            <nav class="navbar navbar-default">
              <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                  <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                  </button>
                  <a class="navbar-brand" href="<?php echo esc_url( home_url( '/' ) ); ?>">MoonLighting</a>
                </div>
                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                  <ul class="nav navbar-nav navbar-right">
                    <li><a href="pages/adultanswers.php">Side Job</a></li>
                    <li class="dropdown">
                      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">My Account <span class="caret"></span></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">Settings</a></li>
                        <li><a href="#">Add Funds</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Sign Out</a></li>
                      </ul>
                    </li>
                  </ul>
                </div>
              </div><!-- /.container-fluid -->
            </nav>
<body <?php body_class(); ?>>
    <div id="wrapper" class="hfeed">
        <header id="header" role="banner">
            <section id="branding">
                <!--I removed this for reasons unrelated-->
            </section>

html元素将文档分为两个主要部分:headbody

head元素包含描述文档本身的元数据信息,或者将其与相关资源(如脚本和样式表)相关联。

身体

这是包含大部分页面的地方。在浏览器窗口(或视窗)中看到的所有内容都包含在这个元素中,包括段落、列表、链接、图像、表格等。body元素有自己的一些独特属性,所有这些属性现在都已弃用,但除此之外,这个元素没什么可说的。页面的外观完全取决于您决定填充的内容;参考按字母顺序排列的所有HTML元素列表,以确定这些内容可能是什么。

详细信息请参见this and this

导航条通常作为<body>标签内的第一项插入。

最小的例子

这是一些非常基本的html

<head>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="container">
    <div class="row">
      <p>I'm the page body</p>
    </div>
  </div>
</body>

在这里插入导航条:

<body>
  <div class="container">
    <div class="row">
      <!-- navbar goes here --> 
      <p>I'm the page body</p>
    </div>
  </div>
</body>