【问题标题】:PHP header - adding different metatags per pagePHP 标头 - 每页添加不同的元标记
【发布时间】:2017-03-14 13:08:42
【问题描述】:

我有一个常用的 php 标头。我想添加页面特定的 META。

目前我的标题是这样的。

<?php include ('includes/pageheader.php'); ?>

目前使用长 if 语句

<?php $page =  $_SERVER['REQUEST_URI']; 
if($page == "/") {
  ?>
<!-- Metadata for home -->
?>

我想知道是否有更好的方法或类可以为此标题构建页面特定的元数据。

【问题讨论】:

  • 试试 Switch 案例
  • 如果您的每个路由/URI 都存储在数据库中,您可以根据请求查找每个页面的元数据。
  • 如果你可以创建像 homeheader.php、mypage1.php、my page2.php 这样的头文件,如果你能识别出确切的页面,你可以使用: 你可以避免很多 if else 语句

标签: php header include meta-tags


【解决方案1】:

在你的标题中,你可以做这样的事情。

<?php if(isset($header)): ?>
  <?php foreach($header as $tag): ?>
    <?= $tag ?>
  <?php endforeach ?>
<?php endif ?>

然后在你的包含之上,你可以添加。

<?php
$header[] = '<meta charset="UTF-8">';
$header[] = '<meta name="description" content="Free Web tutorials">';
$header[] = '<meta name="keywords" content="HTML,CSS,XML,JavaScript">';
$header[] = '<meta name="author" content="John Doe">';
$header[] = '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
include ('includes/pageheader.php');
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2016-04-13
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    相关资源
    最近更新 更多