PHP 中的动态标题标签

发布于 2024-10-04 10:47:46 字数 325 浏览 2 评论 0原文

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

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

标题页面上有以下代码

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

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

谢谢

I am trying to dynamically populate the title tag on a website. I have the following code in my index.php page

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

And the following on my header page

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

But no matter what I do, I cannot get this code to work. Does anyone have any suggestions?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

冷︶言冷语的世界 2024-10-11 10:47:46

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

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

点击此处了解有关变量作用域的更多信息。

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

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

<title>TEST2<?php if ...

This works (tested it - create a new folder, put your first line of code in a file called index.php and the second one in header.php, run it, check the title bar).

You should double check if those two files are in the same folder, and that you're including the right header.php from the right index.php. And ensure that $title is not being set back to null somewhere in your code.

Learn more about Variable Scope here.

Edit: Examples of visible changes would be:

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

<title>TEST2<?php if ...
审判长 2024-10-11 10:47:46

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

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

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

那么它将不起作用 - 你在设置$title变量之前包含头文件并输出标题文本。

Are you including the header file before or after you set the title variable? If you're including it before, then of course it won't be set.

if you're doing something like this in your index.php:

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

then it won't work - you include the header file and output the title text before the $title variable is ever set.

念三年u 2024-10-11 10:47:46

在使用变量之前尝试声明它

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

try to declare the variable before using it

$title = '123';
require 'includes/header.php';
逆光下的微笑 2024-10-11 10:47:46

你好,尝试这个老派方法..

在你的头文件(例如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");


?>

Hi Try this old school method ..

In your Header file (for e.g. 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";
?>

In you PHP Page file do like this (for example 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");


?>
给不了的爱 2024-10-11 10:47:46

我假设您的标头存储在不同的文件中(可能位于根目录之外),那么上述所有解决方案都不适用于您,因为 $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"

这已经过测试并且可以正常工作。

I assume your header is stored in a different file (could be outside the root directory) then all the above solutions will not work for you because $title is set before it is defined.

Here is my solution:
in your header.php file you need to set the $title to be global by: global $title; then echo it in your title so:

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

Then in every page now you can define your title after you have included your header file so for example in your index.php file:

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

This is tested and it is working.

此刻的回忆 2024-10-11 10:47:46

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

做的很简单:

创建一个 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 文件来更新任何标题。

We can also use functions and its a good way to work on real time web sites.

Do simple:

create an index.php file and paste these lines:

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

-- Then

Create a title.php file and paste these lines:

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

It will work perfectly as you want and we can also update any title by touching only one title.php file.

迷你仙 2024-10-11 10:47:46
<?php
    echo basename(pathinfo($_SERVER['PHP_SELF'])['basename'],".php");
?>

这有效。由于我使用的是 PHP,所以我不检查其他扩展;如果需要,请使用 pathinfo['extension']

<?php
    echo basename(pathinfo($_SERVER['PHP_SELF'])['basename'],".php");
?>

This works. Since I'm using PHP I don't check for other extensions; use pathinfo['extension'] in case that's required.

扮仙女 2024-10-11 10:47:46

您可以使用 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 文件将具有标题:用户配置文件
像这样,您可以在网站的任何页面上添加标题

You can achieve that by using define(); function.
In your header.php file add following line :

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

And on that page where you want to set dynamic title, Add following lines:
EX : my page name is user-profile.php where I want to set dynamic title
so I will add those lines that page.

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

So my user-profile/.php file will be having title: User Profile
As like this you can add title on any page on your site

妥活 2024-10-11 10:47:46

示例模板.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 } ?>

Example Template.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 } ?>

忘羡 2024-10-11 10:47:46

分页您的内容

<?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'; ?>

Pages Your Content

<?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'; ?>

半岛未凉 2024-10-11 10:47:46

我在我的项目中使用你的代码,它工作正常

我的代码在 header:

 <title>
  <?php 
   if (isset($title)) {echo $title;}
   else {echo "عنوانی پیدا نشد!";} 
  ?>
 </title>

和我的代码在 index.php:

<?php
  $title = "سرنا صفحه اصلی";
  include("./include/header-menu.php");
?>

I'm using your code in my project and it works properly

My code in header:

 <title>
  <?php 
   if (isset($title)) {echo $title;}
   else {echo "عنوانی پیدا نشد!";} 
  ?>
 </title>

and my code in index.php:

<?php
  $title = "سرنا صفحه اصلی";
  include("./include/header-menu.php");
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文