您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
4-36. 模板文件的拆分和引入(get_header,get_sidebar,get_footer,get_template_part,include)
发布时间:2023-03-19 00:08:33编辑:雪饮阅读()
如果你有dedecms、phpcms、empire cms等模板开发经验。那么这里wordpress我想应该是这些cms中最灵活的。
一般模板开发都是要拆分头部,底部最常见,有的还有侧边栏。
那么对于wordpress中:
对于头部一般约定俗成主题目录中的模板文件应该是header.php
则在主题主文件一般如index.php中加载该header.php则可以用get_header这个函数。
像是同样约定俗成的footer.php则使用get_footer这个函数。
对于这两个函数的调用或许要依赖wp_head、wp_footer
我这里大概看着好像是没有依赖,或许是当加载到当前主题时候,像是这种约定俗成的相关函数已经被注册了吧。
同样是约定俗成的还有侧边栏的sidebar.php则使用get_sidebar函数。
除了约定俗成的函数以外,当然你也可以手动get_template_part函数来调用,传入的参数就是当前主题中模板文件名(不包含.php后缀)
那么当然也支持传统的include(比较推荐的是结合get_theme_file_path获取当前主题绝对路径然后拼接模板文件路径(当然包含.php了,比较传统方法))
当然不使用绝对路径也是可以的,说是会有性能影响(寻址)。
那么如下index.php就包含了以上所述的这些不同的模板拆分加载的使用如
<?php get_header();?>
<body>
<div>
<div class="areaA">
<div class="areaA01">
<?php get_template_part("areaA01");?>
</div>
<div class="areaA02">
<?php include get_theme_file_path().'/areaA02.php';?>
</div>
<div class="areaA03">
<?php include 'areaA03.php';?>
</div>
</div>
<!--areaB假装就是侧边栏-->
<div class="areaB">
<?php get_sidebar();?>
</div>
</div>
<?php get_footer();?>
</body>
</html>
主题目录中的header.php如
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我在网页头部</title>
<?php wp_head();?>
</head>
主题目录中的footer.php如
<?php wp_footer();?>
<div>我在网页底部</div>
主题目录中的sidebar.php如
<div>我是网页侧边栏</div>
主题目录中的areaA01.php如:
<div>我是areaA01</div>
主题目录中的areaA02.php如:
<div>我是areaA02</div>
主题目录中的areaA03.php如:
<div>我是areaA03</div>
关键字词:get_header,get_sidebar,get_footer,get_template_part,include