自动填充其他元标记的元标记

时间:2016-05-15 04:42:59

标签: javascript php jquery html

我正在创建一个包含多个页面的网站..我正在添加必须将META标记添加到每个页面但是我想知道是否有我们从现有标记中提取内容并将其添加到其他内容的方法标签

因此,例如,这些标记已存在于静态HTML页面中:

<meta name="description" content="Description 01"/>
<title>Title 01</title>

现在可以从每个页面中提取这些值,并将其作为其他标记插入到其他页面中。

所以主页已经包含:

<meta name="description" content="Description Home"/>
<title>Title Home</title>

会自动添加这些内容:

<meta itemprop="name" content="Title Home">
<meta itemprop="description" content="Description Home">
<meta name="twitter:title" content="Title Home">
<meta name="twitter:description" content="Description Home">
<meta property="og:title" content="Title Home" />
<meta property="og:description" content="Description Home" />

联系页面已包含:

<meta name="description" content="Description Contact"/>
<title>Title Contact</title>

会自动添加这些内容:

<meta itemprop="name" content="Title Contact">
<meta itemprop="description" content="Description Contact">
<meta name="twitter:title" content="Title Contact">
<meta name="twitter:description" content="Description Contact">
<meta property="og:title" content="Title Contact" />
<meta property="og:description" content="Description Contact" />

1 个答案:

答案 0 :(得分:0)

让它变得动态

创建 header.php ,然后插入以下代码

<!DOCTYPE html>
<html>
    <head>
        <meta name="description" content="<?php echo $page_desc ?>"/>
        <title><?php echo $page_title ?></title>
        <meta itemprop="name" content="<?php echo $page_title ?>">
        <meta itemprop="description" content="<?php echo $page_desc ?>">
        <meta name="twitter:title" content="<?php echo $twt_title ?>">
        <meta name="twitter:description" content="<?php echo $twt_desc ?>">
        <meta property="og:title" content="<?php echo $og_title ?>" />
        <meta property="og:description" content="<?php echo $og_desc ?>" />
    </head>
    <body>

创建 footer.php ,然后插入以下代码

    </body></html>

index.php 设为第一页

<?php
    // the page title and description should be declared first before you include the header and footer
    $page_title = 'My Page Title';
    // same as the others
    $twt_title = $page_title;
    $og_title = $page_title;

    $page_desc = 'My Page Description';
    // same as the others
    $twt_desc = $page_desc;
    $og_desc = $page_desc;

    include 'header.php';
?>
<!-- some page contents -->
<?php include 'footer.php'; ?>

about.php 设为另一个页面

<?php
    // the page title and description should be declared first before you include the header and footer
    $page_title = 'About us';
    // same as the others
    $twt_title = $page_title;
    $og_title = $page_title;

    $page_desc = 'My Page Description';
    // same as the others
    $twt_desc = $page_desc;
    $og_desc = $page_desc;

    include 'header.php';
?>
<!-- some page contents -->
<?php include 'footer.php'; ?>