创建 PHP 页眉/页脚
我正在为一个朋友设计一个相对简单的网站.我想实现 php,这样他就可以更改他的页眉/页脚,而不必返回每个文件.问题是,我对 php 的工作原理几乎完全不熟悉.有没有一种简单的方法可以做到这一点?我已经看到了一些关于如何制作 php 标头的答案,但它们看起来都不同,而且我没有取得太大的成功.我并不是说它们不起作用(我可能做错了),但在这种情况下越简单越好.
I'm designing a relatively simple site for a friend. I would like to impliment php so that he can change his header/footer without having to go back through every file. Problem is, I'm pretty much totally unfamiliar with how php works. Is there a simplistic way to do this? I've seen a few answers for how to make a php header but they all seem different and I haven't had much success. I'm not saying they don't work (I probably did it wrong) but the simpler in this case, the better.
谢谢!
推荐答案
除了使用 include()
或 include_once()
来包含页眉和页脚之外,还有一件事我发现有用的是能够为每个页面包含自定义页面标题或自定义标题标签,但仍然在部分包含中包含标题.我通常这样做如下:
Besides just using include()
or include_once()
to include the header and footer, one thing I have found useful is being able to have a custom page title or custom head tags to be included for each page, yet still have the header in a partial include. I usually accomplish this as follows:
在网站页面中:
<?php
$PageTitle="New Page Title";
function customPageHeader(){?>
<!--Arbitrary HTML Tags-->
<?php }
include_once('header.php');
//body contents go here
include_once('footer.php');
?>
并且,在 header.php 文件中:
And, in the header.php file:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title><?= isset($PageTitle) ? $PageTitle : "Default Title"?></title>
<!-- Additional tags here -->
<?php if (function_exists('customPageHeader')){
customPageHeader();
}?>
</head>
<body>
可能有点超出您原始问题的范围,但允许使用包含更多的灵活性很有用.
Maybe a bit beyond the scope of your original question, but it is useful to allow a bit more flexibility with the include.
相关文章