【发布时间】:2011-04-13 09:04:55
【问题描述】:
我们在我们网站的所有页面中都包含一个 header.php 文件。因此,我们可以在 header.php 文件中放置一个单独的标题,该文件将应用于整个站点,或者在每个页面中都有一个自定义标题以更具描述性。
问题是这样做时,标题会在头标签之外(保留在 header.php 文件中)并被标记为无效。
是否有某种函数可以用来在页面中定义一个变量($pageTitle),它会显示在 head 标签中?
谢谢。
【问题讨论】:
我们在我们网站的所有页面中都包含一个 header.php 文件。因此,我们可以在 header.php 文件中放置一个单独的标题,该文件将应用于整个站点,或者在每个页面中都有一个自定义标题以更具描述性。
问题是这样做时,标题会在头标签之外(保留在 header.php 文件中)并被标记为无效。
是否有某种函数可以用来在页面中定义一个变量($pageTitle),它会显示在 head 标签中?
谢谢。
【问题讨论】:
嗯.....
<?php
$pageTitle = "Test";
include('header.php');
?>
编辑
然后在你的 header.php 中
<head>
<title><?php echo $pageTitle; ?> </title>
</head>
【讨论】:
您似乎想要在某些页面上使用动态标题?
<?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>
【讨论】:
其实应该是这样的
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";
?>
模板.php:
<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file ?>
</body>
tpl.news.php
<h1><?=$page_title?></h1>
<?=$body?>
简单明了
【讨论】:
在我看来,您仍然可以在标题中完成所有这些工作:
<?php
include(...your config/connect file...);
mysql_query(... get page variables ...);
$pageTitle = stripslashes($titlefromDB);
?>
<html><head><title><?php echo $pageTitle; ?></head>
这样就结束了 header.php。现在将它包含在您要使用的每个页面上,然后使用您的<body></body></html>。
只是一个想法,但无论如何你要解决这个问题,你必须先连接到你的数据库,检查页面是否存在,如果存在则将标题设置为变量,然后开始构建 html。
【讨论】: