当自定义WordPress主题时,我应该将导航栏放在`<body>`标签之前还是之后?

6
我正在通过创建子主题来自定义 WP 主题。我将 Bootstrap 的导航栏放置在子主题目录中的 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>
2个回答

12
html元素将文档分为两个主要部分: headbodyhead head元素包含元数据——描述文档本身或将其与相关资源(如脚本和样式表)关联的信息。 body 这里包含页面中大部分内容。浏览器窗口(或视口)中可以看到的所有内容都包含在此元素中,包括段落、列表、链接、图像、表格等等。body元素具有一些独特的属性,现已全部过时,但除此之外,很少有什么需要说的了。页面的外观将完全取决于您决定填充它的内容; 参考所有HTML元素的字母顺序列表,以确定这些内容可能是什么。
更多详情请参见这里这里

2
导航栏通常作为<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接