如何在网站的每个页面上添加导航栏


How do I add a navbar on every page of my website?

你好,我是创建网站的新手,最近一直在搞乱HTMl和CSS。 我创建了一个导航栏,并希望将其放在每个页面上。 我以前研究过这个问题,但就是想不通。 它只是没有放在页面上的导航栏中。

首页.html

                            <!DOCTYPE html>
                            <html>
                            <head>
                            <link rel="stylesheet" type="text/css" href="style.css">
                            </head>
                            <body>
                            <?php include 'navbar.php';?>
                            <div id="hbox1">
                            <h2>Random</h2>
                            <p>Is he staying arrival address earnest. To preference considered it themselves inquietude collecting estimating. View park for why gay knew face. Next than near to four so hand. Times so do he downs me would. Witty abode party her found quiet law. They door four bed fail now have. <a href="#">here.</a></p>
                            </div>
                            <div id="hbox2">
                            <h2>Random</h2>
                            <p>Is he staying arrival address earnest. To preference considered it themselves inquietude collecting estimating. View park for why gay knew face. Next than near to four so hand. Times so do he downs me would. Witty abode party her found quiet law. They door four bed fail now have. <a href="#">here.</a></p>
                            </div>
                            <div id="hbox3">
                            <h2>Random</h2>
                            <p>Is he staying arrival address earnest. To preference considered it themselves inquietude collecting estimating. View park for why gay knew face. Next than near to four so hand. Times so do he downs me would. Witty abode party her found quiet law. They door four bed fail now have. <a href="#">here!</a></p>
                            </div>
                            <div id="hbox4">
                            <h2>Random</h2>
                            <p>Is he staying arrival address earnest. To preference considered it themselves inquietude collecting estimating. View park for why gay knew face. Next than near to four so hand. Times so do he downs me would. Witty abode party her found quiet law. They door four bed fail now have. <a href="#">here.</a></p>
                            </div>
                            </body>
                            </html>

和导航栏.php

                            <h1>&nbsp&nbspMy Homepage</h1>
                            <ul id="nav">
                            <li><a href="#">Random</a></li>
                            <li><a href="#">Random</a></li>
                            <li><a href="#">Random</a></li>
                            <li><a href="#">Random</a></li>
                            </ul>

你的代码不工作的原因是因为你试图使用.html文档解析PHP代码。

要么指示 Apache 将.html文件视为 PHP,要么home.html重命名为 home.php


.html文件视为 PHP。

在服务器根目录下的.htaccess中:

<FilesMatch "'.(htm|html|php)$">
SetHandler application/x-httpd-php
</FilesMatch> 

或单个文件名:

<Files yourpage.html>
AddType application/x-httpd-php .html
</Files>

或多个文件名:

<FilesMatch "^(file_one|file_two|file_three)'.html$">
  AddType application/x-httpd-php .html
</FilesMatch>

AddType application/x-httpd-php .php .htm .html
AddHandler x-httpd-php .php .htm .html

对于由HostGator托管的人:来源

AddHandler application/x-httpd-php5 .html .htm

如果你使用 XAMPP 在本地开发,你应该使用 AddHandler 而不是 AddType

AddHandler application/x-httpd-php .html .htm

弗雷德是对的,你的文件扩展名必须注册为可由php解释。

我个人建议你为.php更改主文件的扩展名,而不是配置你的Web服务器来解释html。

您还需要考虑使用模板引擎(如Twig或Smarty),这将允许您扩展内容。通常的做法是使用定义 narbar、工具栏或其他页眉/页脚的基本模板,并使用其他文件对其进行扩展。

下面是 Twig 的示例:

基地.html

<html>
  <body>
    <your_navbar_markup />
{% block body %}{% endblock %}
  </body>
</html>

内容1.html

{% extends 'base.html' %}
{% block body %}
  My pretty content1
{% endblock %}