PHP 中的动态标题标签
我正在尝试动态填充网站上的标题标签。我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这有效(测试过 - 创建一个新文件夹,将第一行代码放入名为 index.php 的文件中,将第二行代码放入 header.php 中,运行它,检查标题栏)。
您应该仔细检查这两个文件是否位于同一文件夹中,并且您是否包含正确的 header.php 来自正确的 index.php。并确保 $title 没有在代码中的某处设置回
null
。点击此处了解有关变量作用域的更多信息。
编辑:可见更改的示例是:
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:
您是在设置标题变量之前还是之后包含头文件?如果您之前包含它,那么它当然不会被设置。
如果你在你的index.php中做这样的事情:
那么它将不起作用 - 你在设置$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:
then it won't work - you include the header file and output the title text before the $title variable is ever set.
在使用变量之前尝试声明它
try to declare the variable before using it
你好,尝试这个老派方法..
在你的头文件(例如header.php)中
在你的PHP页面文件中这样做(例如about.php)
Hi Try this old school method ..
In your Header file (for e.g. header.php)
In you PHP Page file do like this (for example about.php)
我假设您的标头存储在不同的文件中(可能位于根目录之外),那么上述所有解决方案都不适用于您,因为
$title
是在定义之前设置的。这是我的解决方案:
在您的
header.php
文件中,您需要通过以下方式将$title
设置为全局:global $title;
然后在您的标题中回显它,以便:然后在每个页面中,现在您可以在包含头文件后定义标题,例如在
index.php
文件中:这已经过测试并且可以正常工作。
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: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:This is tested and it is working.
我们还可以使用函数,这是在实时网站上工作的好方法。
做的很简单:
创建一个 index.php 文件并粘贴这些行:
-- 然后
创建一个 title.php 文件并粘贴这些行:
它将按照您的需要完美工作,我们还可以通过仅触摸一个 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:
-- Then
Create a title.php file and paste these lines:
It will work perfectly as you want and we can also update any title by touching only one title.php file.
这有效。由于我使用的是 PHP,所以我不检查其他扩展;如果需要,请使用
pathinfo['extension']
。This works. Since I'm using PHP I don't check for other extensions; use
pathinfo['extension']
in case that's required.您可以使用
define();
函数来实现这一点。在您的
header.php
文件中添加以下行:在要设置动态标题的页面上,添加以下行:
EX:我的页面名称是
user-profile.php
我想在其中设置动态标题所以我将在该页面添加这些行。
因此我的
user-profile/.php
文件将具有标题:用户配置文件像这样,您可以在网站的任何页面上添加标题
You can achieve that by using
define();
function.In your
header.php
file add following line :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 titleso I will add those lines that page.
So my
user-profile/.php
file will be having title: User ProfileAs like this you can add title on any page on your site
示例模板.php
Example Template.php
分页您的内容
Pages Your Content
我在我的项目中使用你的代码,它工作正常
我的代码在 header:
和我的代码在 index.php:
I'm using your code in my project and it works properly
My code in header:
and my code in index.php: