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 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入