从PHP文件中读取元标记

时间:2015-10-18 22:19:35

标签: php

我正在尝试从与正在加载的页面相关的php文件中加载元标记。

元代码可以工作,但前提是我将元代码添加到每个页面。

  1. 我不想将元代码添加到每个页面。
  2. 我想向我们提供包含此代码的
  3. 但是包含没有填充。
  4. 以下是代码:

    1. 如果我将其添加到每个页面,此代码也会有效。
    2. 如果我删除并添加包含此代码的,则不会填充元数据。

          <?php include("includes/metatgs.php"); ?> 
          <?php if ($page = basename(__FILE__)); ?>
      <?php { ?>
          <meta charset='<?php echo $meta[$page]['charset']; ?>'>
          <title><?php echo $meta[$page]['title']; ?></title>
          <meta name='description' content='<?php echo $meta[$page]['description'];?>'>
          <meta name='keywords' content='<?php echo $meta[$page]['keywords']; ?>'>
      

      metatgs.php:这就是我用来填充元数据的方法。

      $meta['index.php']['charset'] = "utf-8";
      $meta['index.php']['title'] = "Home";
      $meta['index.php']['description'] = "The Description";
      $meta['index.php']['keywords'] = "keywords";
      
      $meta['about.php']['charset'] = "utf-8";
      $meta['about.php']['title'] = "About";
      $meta['about.php']['description'] = "The Description";
      $meta['about.php']['keywords'] = "keywords";
      
    3. 如何将此代码添加到包含并填充元数据?

3 个答案:

答案 0 :(得分:1)

您可以采取以下措施将正确的元标记带到正确的页面。

  1. 创建一个文件夹并将其命名为meta_tags
  2. 根据您要使用元标记的页面在其中创建php页面.example index_meta_tags.php,about_meta_tags.php,contact_meta_tags.php
  3. 在您所在的页面中。 index.php你想使用index_meta_tags.php 所以你可以写这个代码
  4. $current_page = basename($_SERVER['PHP_SELF']); //get currect page name ex. index.php
    
    if(current_page == "index.php"){<br/>
         include "index_meta_tags.php";<br/>
    }else if(current_page == "about.php"){<br/>
         include "about_meta_tags.php";<br/>
    }else if(current_page == "contact.php"){<br/>
         include "contact_meta_tags.php";<br/>
    }
    

    因此,您将拥有基于页面

    的正确元标记

答案 1 :(得分:0)

根据您的帖子和评论,我的猜测是$page不是您所期望的。由于您没有使用包含的任何类型的函数,因此变量范围不是问题。根据您的error_reporting()设置,PHP不会为未定义的索引引发错误。

我建议您修改error_reporting()设置并查看$page值是什么。然后确保您实际使用$meta

为该页面定义数组

答案 2 :(得分:0)

答案是 - 我换了

if-else

用这个

<?php if ($page = basename(__FILE__)); ?>   

自从他提到($ _SERVER ['PHP_SELF'])后,我可能会给予Programmatistis。但是真正考虑它,因为他想重写世界并增加10页而不是我的简单解决方案。