doufu2496 2016-07-24 09:50
浏览 29
已采纳

在PHP模板的head部分中包含元标记的最佳方法是什么?

I created a simple php template.

In the header.php I have the following lines of code:

<!DOCTYPE HTML>
<html lang="en"><head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
<meta name="description" content="<?php echo $description ?>"> 
<link rel="canonical" href="<?php echo $canonical ?>">
</head><body>

here rest of code (header, menu etc...)


On all pages I have the following lines of code at the top:

<?php
$title="";
$description="";
$canonical="";
$page_schema="http://schema.org/WebPage";
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/includes/";
require($INC_DIR. "header.php"); ?>

I have pages with Open Graph meta tags and some pages who don't have Open Graph meta tags.

I have pages with

<link rel="alternate" hreflang="nl" href="">

and pages that don't use this.

I have 2 pages with robots meta tag noindex follow and many pages who don't use any robots meta tag.

Now i'm using the following option:

I've added the following line in the header.php

<?php echo $meta_tags ?> 

Example:

<!DOCTYPE HTML>
<html lang="en"><head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
<meta name="description" content="<?php echo $description ?>"> 
<link rel="canonical" href="<?php echo $canonical ?>">
<?php echo $meta_tags ?> 

And the following lines on pages (not all use hreflang tag):

$meta_tags='<link rel="alternate" hreflang="nl" href="">
<meta property="og:image" content="">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:url" content="">';

Example:

<?php
$title="";
$description="";
$canonical="";
$meta_tags='<link rel="alternate" hreflang="nl" href="">
<meta property="og:image" content="">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:url" content="">';
$page_schema="http://schema.org/WebPage";
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/includes/";
require($INC_DIR. "header.php"); ?>

Is this a good solution to include some meta tags on pages that do use them? Or is there a better way?

Like adding NULL (without quotes) to empty variables like: $title=NULL;

Example (for a few pages that don't use opengraph):

header.php:

<meta property="og:title" content="<?php echo $og_title ?>">
<meta property="og:description" content="<?php echo $og_description ?>">
<meta property="og:url" content="<?php echo $og_url ?>">';

on pages (which don't use open graph tags):

$og_title=NULL;
$og_description=NULL;
$og_url=NULL;

What is the best solution? Is there a better solution?

Btw... I was following the steps of this tutorial here: http://www.heliomedia.com/tutorials/html5-template-with-seo-friendly-variables/

  • 写回答

1条回答 默认 最新

  • dougui5419 2016-07-24 10:48
    关注

    Yes, omit the variables you're not using in each page. In your head include, wrap each meta tag in a check to see if it's used, e.g.:

    <?php if (isset($og_title)) {?>
        <meta property="og:title" content="<?php echo htmlspecialchars($og_title);?>">
    <?php }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题