【问题标题】:Dynamic Title Tag in PHPPHP中的动态标题标签
【发布时间】:2011-05-16 22:39:45
【问题描述】:

我正在尝试动态填充网站上的标题标签。我的 index.php 页面中有以下代码

<?php $title = 'myTitle'; include("header.php"); ?>

我的标题页上还有以下内容

<title><?php if (isset($title)) {echo $title;}
else {echo "My Website";} ?></title>

但无论我做什么,我都无法让这段代码工作。有没有人有什么建议?

谢谢

【问题讨论】:

  • 没什么。即使我设置了 $title 我总是得到默认值。
  • 您的&lt;head&gt; 中是否还有另一个&lt;title&gt; 标签?
  • 不,只出现一个 标签。
  • 要查看是否有隐蔽的错误,请将“我的网站”更改为其他内容,然后查看它是否出现在浏览器中。哦,也许它只是在浏览器缓存中?
  • 当我将“我的网站”更改为其他内容时

标签: php dynamic title


【解决方案1】:

这个工作(测试它 - 创建一个新文件夹,将你的第一行代码放在一个名为 index.php 的文件中,第二行放在 header.php 中,运行它,检查标题栏)。

您应该仔细检查这两个文件是否在同一个文件夹中,以及您是否包含正确的 header.php from 正确的 index.php。并确保 $title 没有在代码中的某处被设置回 null

详细了解变量作用域here

编辑:可见更改的示例是:

TEST1<?php $title = 'myTitle'; include("header.php"); ?>

<title>TEST2<?php if ...

【讨论】:

    【解决方案2】:

    您是在设置标题变量之前还是之后包含头文件?如果你之前包含它,那么它当然不会被设置。

    如果你在 index.php 中做这样的事情:

    <?php
         include('header.php');
         $title = "blah blah blah";
    ?>
    

    那么它就行不通了——你在 $title 变量设置之前包含头文件并输出标题文本。

    【讨论】:

    • 我之前设置的,我包含了。
    • 头文件中是否有东西将 $title 重置为空?
    • 这个是最好的解决方案
    【解决方案3】:

    嗨试试这个老式的方法..

    在您的头文件中(例如 header.php)

    <?php 
    error_reporting(E_ALL);
    
    echo '<!DOCTYPE html>
    <!--[if IE 7 ]><html class="ie7" lang="en"><![endif]-->
    <!--[if IE 8 ]><html class="ie8" lang="en"><![endif]-->
    <!--[if IE 9 ]><html class="ie9" lang="en"><![endif]-->
    <!--[if (gte IE 10)|!(IE)]><!-->
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
    <!--<![endif]-->
    <head>'; 
    ?>
    <?php  
    if($GLOBALS['title']) {
        $title = $GLOBALS['title'];
    } else {
        $GLOBALS['title'] = "Welcome to My Website";        
    }
    if($GLOBALS['desc']) {  
        $desc = $GLOBALS['desc'];
    } else {
        $desc = "This is a default description of my website";      
    }
    if($GLOBALS['keywords']) {
        $keywords = $GLOBALS['keywords'];   
    } else {
        $keywords = "my, site, key, words";     
    }
    echo "\r\n";
    echo "<title> ". $title ." | MyWebsite.com </title>";
    echo "\r\n";
    echo "<meta name=\"description\" content='". $GLOBALS['title']."'>";
    echo "\r\n";
    echo "<meta name=\"keywords\" content='".$GLOBALS['title']."'>";
    echo "\r\n";
    ?>
    

    在你的 PHP 页面文件中这样做(例如 about.php)

    <?php
    
    $GLOBALS['title'] = 'About MyWebsite -This is a Full SEO Title';
    $GLOBALS['desc'] = 'This is a description';
    $GLOBALS['keywords'] ='keyword, keywords, keys';
    
    include("header.php");
    
    
    ?>
    

    【讨论】:

      【解决方案4】:

      我假设您的标头存储在不同的文件中(可能位于根目录之外),那么上述所有解决方案都不适用于您,因为$title 是在定义之前设置的。

      这是我的解决方案: 在您的 header.php 文件中,您需要将 $title 设置为全局:global $title; 然后在您的标题中回显它:

      <?php global $title; ?>
      <title><?php echo isset($title) ? $title : "{YOUR SITE NAME}"; ?></title>
      

      然后在每个页面中,您可以在包含头文件后定义标题,例如在 index.php 文件中:

      include_once("header.php");
      $title = "Your title for better SEO"
      

      这是经过测试的,它正在工作。

      【讨论】:

        【解决方案5】:

        尝试在使用前声明变量

        $title = '123';
        require 'includes/header.php';
        

        【讨论】:

          【解决方案6】:

          我们还可以使用函数及其在实时网站上工作的好方法。

          做简单的事:

          创建一个 index.php 文件并粘贴以下行:

          <?php include("title.php");?>
          <!doctype html>
          <html>
          <head>
              <title><?php index_Title(); ?></title>
          <head>
          </html>
          

          -- 那么

          创建一个 title.php 文件并粘贴这些行:

          <?php
          function index_Title(){
          $title = '.:: itsmeShubham ::.';
          if (isset($title)){
              echo $title;
              }else{
                  echo "My Website";
              };
          }
          ?>
          

          它会按照您的需要完美运行,我们还可以通过触摸一个 title.php 文件来更新任何标题。

          【讨论】:

            【解决方案7】:
            <?php
                echo basename(pathinfo($_SERVER['PHP_SELF'])['basename'],".php");
            ?>
            

            这行得通。因为我使用的是 PHP,所以我不检查其他扩展;如果需要,请使用pathinfo['extension']

            【讨论】:

              【解决方案8】:

              您可以使用define(); 函数来实现。
              在您的 header.php 文件中添加以下行:

              <title><?php echo TITLE; ?></title>
              

              在您要设置动态标题的页面上,添加以下行:
              EX:我的页面名称是user-profile.php,我想在其中设置动态标题 所以我将在该页面添加这些行。

              <?php
                  define('TITLE','User Profile'); //variable which is used in header.php
                  include('header.php');
                  include('dbConnection.php');
              ?>
              

              所以我的user-profile/.php 文件将具有标题:用户个人资料
              像这样,您可以在您网站的任何页面上添加标题

              【讨论】:

                【解决方案9】:

                示例模板.php

                <?php
                if (!isset($rel)) {$rel = './';}
                if (!isset($header)) {
                  $header = true;
                ?><html>
                  <head>
                    <title><?php echo $pageTitle; ?></title>
                  </head>
                <body>
                
                <?php } else { ?>
                
                </body>
                </html><?php } ?>

                【讨论】:

                • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
                【解决方案10】:

                为您的内容分页

                <?php
                  $rel = './'; // location of page relative to template.php
                  $pageTitle = 'This is my page title!';
                  include $rel . 'template.php';
                ?>
                
                Page content here
                
                <?php include $rel . 'template.php'; ?>

                【讨论】:

                  猜你喜欢
                  • 2011-05-02
                  • 2021-10-12
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-23
                  • 1970-01-01
                  • 2012-06-20
                  相关资源
                  最近更新 更多