PHP 将页面生成的标题放入 <head> [英] PHP to place page-generated title into <head>

查看:59
本文介绍了PHP 将页面生成的标题放入 <head>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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

谢谢.

推荐答案

其实应该是这样

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:

template.php:

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

tpl.news.php

tpl.news.php

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

简单明了

这篇关于PHP 将页面生成的标题放入 &lt;head&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆