PHP header - 每页添加不同的元标签
PHP header - adding different metatags per page
我有一个共同点phpheader。我想添加页面特定的 META。
目前我的 header 看起来像这样。
<?php include ('includes/pageheader.php'); ?>
目前使用长if语句
<?php $page = $_SERVER['REQUEST_URI'];
if($page == "/") {
?>
<!-- Metadata for home -->
?>
我想知道是否有更好的方法或 class 能够为这个 header 构建页面特定的元数据。
在你的页眉中,你可以做这样的事情。
<?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');
?>
我有一个共同点phpheader。我想添加页面特定的 META。
目前我的 header 看起来像这样。
<?php include ('includes/pageheader.php'); ?>
目前使用长if语句
<?php $page = $_SERVER['REQUEST_URI'];
if($page == "/") {
?>
<!-- Metadata for home -->
?>
我想知道是否有更好的方法或 class 能够为这个 header 构建页面特定的元数据。
在你的页眉中,你可以做这样的事情。
<?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');
?>