HTML / PHP: HTML 头部的哪些部分可以保存在单独的文件中

HTML / PHP: Which parts of an HTML head can be saved in a separate file

我是 HTML 的新手,希望有人能帮助我。

我正在尝试建立一个网站,其中每个页面的代码都保存为单独的 php 文件。 由于所有这些页面都使用相同的 文档头 并包含 我想从页面中尽可能多地删除它并将其保存在单独的包含文件中 (header.php) 然后我可以使用 PHP 包含它。

我现在的头看起来如下,因为这在所有页面上都是一样的,我目前将所有这些保存在单独的 header.php 文件中,然后只在单个页面上使用 <?php include "header.php"; ?>

谁能告诉我应该将其中的哪些部分保留在单个页面上以进行正确设置,以及是否应该在此处更改或添加任何内容?

注: jQuery 和 JS 分别包含在页脚中。

我的PHP(头文件):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="author" content="John Doe" />
        <meta name="description" content="Created: 2015-06" />

        <base href="http://www.MyURL.com" target="_self" />

        <!-- CSS -->        
        <link rel="stylesheet" type="text/css" href="includes/styles.css" />
        <!-- CSS - Font Awesome -->
        <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />

        <title>My Page</title>
    </head>

    <body>
        <div class="wrapper">
        <!-- ... -->

这应该给你大概的想法...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="author" content="John Doe" />
    <meta name="description" content="Created: 2015-06" />
    <base href="http://www.MyURL.com" target="_self" />

    <!--
    Your paths need to relative to the websites root directory or the full
    URL else they may not work depending on where the file is located.
    -->     
    <link rel="stylesheet" type="text/css" href="/includes/styles.css" />
    <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />

    <!-- 
    This line just echos the title if one has been set or a default string
    if the title hasn't been set. You will need to ensure every page that
    includes this file sets the title before including this file. You may
    also want to take this approach with other values.
    -->
    <title><?php echo isset($title) ? $title : 'Default Title'; ?></title>
</head>
<body>
    <div class="wrapper">

由于您在 header.php 中开始您的标签,您可以在自己的页面上关闭标签(我指的是其他页面,即:联系方式、关于我们、产品等...)。

你在问题中提到的代码部分可以称为 header.php 并且可以进一步包含在需要它的每个页面上。

除此之外

  1. Header - header.php
  2. 主要内容 - main-content.php - 这将特定于您网站的每个页面
  3. 页脚 - footer.php - 这也可以通用 - 并进一步包含在每个页面中。
  4. 除此之外,您还可以拥有更多 php 文件,其中可能包含导航或任何类型的常见内容块。

示例页面可以是:

<?php
include 'header.php'
/*either add here the content or include further php files*/
include 'about-us.php'
include 'footer.php'
?>

你的顶头文件(top_head.php):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <?php if(isset($page_author)){ ?>
            <meta name="author" content="<?php echo $page_author; ?>" />
        <?php } ?>

        <?php if(isset($page_description)){ ?>
            <meta name="description" content="Created: 2015-06" />
        <?php } ?>

        <!-- CSS -->        
        <link rel="stylesheet" type="text/css" href="includes/styles.css" />
        <!-- CSS - Font Awesome -->
        <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />

        <title><?php echo $page_title; ?></title>

你当前的头文件:

<?php
      $page_title = 'Hello';
      $page_description = 'This is a beautiful description';
?>
<?php include("top_head.php"); ?>
</head>

    <body>
        <div class="wrapper">
        <!-- ... -->

你做得对,所有页面之间共享的部分应该放在一个单独的 php 文件中,这大大改善了你维护网站时的生活(想象一下添加样式表或一段时间后您网站的每个页面都有一个图书馆...)。

为了帮助 SEO,您应该指定一个标题,也许还有一些特定于页面的元标记。

还要仔细考虑是否有其他部分在每个页面中重复,例如左栏或右栏或特定小部件(例如横幅图像或类似的东西)