PHP 将页面生成的标题放入中

发布于 2024-09-19 17:12:11 字数 238 浏览 5 评论 0原文

我们在网站的所有页面上都包含一个 header.php 文件。因此,我们可以在 header.php 文件中放置一个标题,该标题将应用于整个站点,或者在每个页面中添加一个自定义标题以更具描述性。

问题是,这样做时,标题将位于 head 标签之外(保留在 header.php 文件中)并被标记为无效。

我们是否可以使用某种函数在页面内定义一个变量($pageTitle),该变量将显示在 head 标签中?

谢谢。

We include a header.php file across all the pages on our site. So we could either place a single title in the header.php file which would be applied to the entire site, or have a custom header within each page to be more descriptive.

The problem is that in doing that, the title would be outside the head tags (which remain in the header.php file) and marked as invalid.

Is there some kind of function we can use to define a variable ($pageTitle) within the page, that will be displayed in head tag?

Thanks.

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

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

发布评论

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

评论(4

没︽人懂的悲伤 2024-09-26 17:12:11

其实应该是这样的

news.php:

<?
include "config.php"; //connect to database HERE.
$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);

$tpl_file = "tpl.news.php";
include "template.php";
?>

template.php:

<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file ?>
</body>

tpl.news.php

<h1><?=$page_title?></h1>
<?=$body?>

简单明了

Actually it should be this way

news.php:

<?
include "config.php"; //connect to database HERE.
$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);

$tpl_file = "tpl.news.php";
include "template.php";
?>

template.php:

<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file ?>
</body>

tpl.news.php

<h1><?=$page_title?></h1>
<?=$body?>

plain and simple

陪我终i 2024-09-26 17:12:11

嗯......

<?php 
$pageTitle = "Test";
include('header.php');
?>

编辑

然后在你的 header.php 中

<head>
    <title><?php echo $pageTitle; ?> </title>
</head>

Ummm.....

<?php 
$pageTitle = "Test";
include('header.php');
?>

EDIT

Then in your header.php

<head>
    <title><?php echo $pageTitle; ?> </title>
</head>
記憶穿過時間隧道 2024-09-26 17:12:11

看起来您想要在某些页面上使用动态标题?

<?php
$defaultPageTitle='Default Title'; //Default title
include('header.php');
?>

<?php
/* You would define $newPageTitle here if necessary
 (i.e. use $_SERVER['REQUEST_URI'] to get the URL
 and check a database for the $newPageTitle */
?>
<head>
<?php
if(isset($newPageTitle)){echo '<title>'.$newPageTitle.'</title>';}
else{echo '<title>'.$defaultPageTitle.'</title>';}
?>
</head>

It looks like you want a dynamic title on some pages?

<?php
$defaultPageTitle='Default Title'; //Default title
include('header.php');
?>

<?php
/* You would define $newPageTitle here if necessary
 (i.e. use $_SERVER['REQUEST_URI'] to get the URL
 and check a database for the $newPageTitle */
?>
<head>
<?php
if(isset($newPageTitle)){echo '<title>'.$newPageTitle.'</title>';}
else{echo '<title>'.$defaultPageTitle.'</title>';}
?>
</head>
剩一世无双 2024-09-26 17:12:11

在我看来,您仍然可以在标头中完成所有这些工作:

<?php
include(...your config/connect file...);
mysql_query(... get page variables ...);
$pageTitle = stripslashes($titlefromDB);
?>
<html><head><title><?php echo $pageTitle; ?></head>

header.php 就这样结束了。现在,将其包含在您想要使用它的每个页面上,然后添加您的

只是一个想法,但无论如何你要解决这个问题,你必须首先连接到你的数据库,检查页面是否存在,如果存在,则将标题设置为变量,然后开始构建 html。

The way I see it, you can still do all this work in your header:

<?php
include(...your config/connect file...);
mysql_query(... get page variables ...);
$pageTitle = stripslashes($titlefromDB);
?>
<html><head><title><?php echo $pageTitle; ?></head>

Thus concludes you header.php. Now include this on every page you want to use it, and follow with your <body></body></html>.

Just one idea, but anyway you go around this, you're going to have to connect to your DB first, check if page exists, if so set title as variable, then begin constructing html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文