您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程

WordPress页面模板是从哪里来的

帅黎2025-03-24WordPress教程已有人查阅

导读今天看到WordPress中的可以自定义页面模板,很好奇在添加页面时,这些页面模板的选项从哪里来的?首先找到Post.php,竟然没有找到这区域是在post.php的那个函数内输出的,后来通过查

今天看到WordPress中的可以自定义页面模板,很好奇在添加页面时,这些页面模板的选项从哪里来的?
首先找到Post.php,竟然没有找到这区域是在post.php的那个函数内输出的,后来通过查找输出页面Html代码,找到了输出位置是在wp-admin/includes/meta-boxes.php的“page_attributes_meta_box”方法中。
page_attributes_meta_box方法就是用来显示页面属性,它被“edit-form-advanced.php”页面所调用,使用的语句是:
add_meta_box('pageparentdiv', 'page' == $post_type ? __('Page Attributes') : __('Attributes'), 'page_attributes_meta_box', $post_type, 'side', 'core');
通过add_meta_box来调用了“page_attributes_meta_box”。meta-boxes.php是一个插件,是在”wp-admin\edit-form-advanced.php”通过require_once('./includes/meta-boxes.php');引入到程序中的。而”wp-admin\edit-from-advanced.php”也是通过require_once被包含在post.php中。
在meta-boxes.php中,使用了page_template_dropdown方法来输出select的内容。
<select name="page_template" id="page_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown($template); ?>
</select>
在文件”wp-admin/includes/template.php "中找到了这个方法,而在wp-admin\includes\admin.php":require_once(ABSPATH . 'wp-admin/includes/template.php');中引入了template.php这个文件到wordpress中。在template.php中,page_template_dropdown方法主要是通过”get_page_templates“方法获取到option的值。get_page_templates是来自于”wp-admin/includes/theme.php”文件的。
function get_page_templates() {
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$page_templates = array();
if ( is_array( $templates ) ) {
$base = array( trailingsla (get_template_directory()), trailingsla (get_stylesheet_directory()) );
foreach ( $templates as $template ) {
$basename = str_replace($base, '', $template);
// don't allow template files in subdirectories
if ( false !== strpos($basename, '/') )
continue;
$template_data = implode( '', file( $template ));
$name = '';
if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
$name = _cleanup_header_comment($name[1]);
if ( !empty( $name ) ) {
$page_templates[trim( $name )] = $basename;
}
}
}
//print_r($page_templates);
// die();
return $page_templates;
}
在这个方法中,首先通过get_themes方法获取所有的模板,然后再通过get_current_theme()获取到当前的模板保存信息。通过输出$theme的内容,我们获取的是一个所有模板的目录列表。wordpress是通过什么来确定哪个模板是页面模板?
通过”$template_data = implode( '', file( $template )); “方法来获取模板的内容。然后通过正则匹配有在模板内容中指定”Template Name“的模板,获取的模板名后,这个模板就是页面模板。我们可以打开wordpress默认自带的模板”onecolumn-page.php“在这个模板的注释部分可以看到” * Template Name: One column, no sidebar“我们如果需要给wordpress增加新的页面模板的话,就需要在模板中带上Template Name这个注释,wordpress就自动会把这个模板识别成页面模板。
Tips
可以通过在wp-config.php中把define('WP_DEBUG', false);设置为true,就可以通过print_r和die来逐步输出我们需要查看的内容。

本文标签:

很赞哦! ()

相关源码

  • (PC+WAP)激光水幕音乐喷泉设备工程网站源码下载本模板基于PbootCMS系统开发,为喷泉设备工程类企业设计,特别适合展示音乐喷泉、激光水幕等水景艺术项目。采用响应式技术,确保各类工程案例在不同设备上都能呈现视觉效果。查看源码
  • (自适应响应式)超市仓储仓库货架展架网站pbootcms源码下载本模板为货架展架、仓储货架行业量身打造,采用PbootCMS内核开发,充分考虑了货架产品展示和企业形象展示的需求。模板设计简洁大方,突出产品特点,能够有效展示各类货架产品的规格参数和应用场景,帮助访客快速了解企业核心业务和产品优势。查看源码
  • (自适应)游泳馆泳池水处理器设备pbootcms网站模板免费下载专注服务于泳池水处理设备制造商、泳池系统工程商等企业用户。模板预设设备分类体系、技术参数展示模块和解决方案呈现页面,满足行业特有展示需求。查看源码
  • (PC+WAP)蓝色五金机械设备营销型网站源码下载基于PbootCMS内核开发的营销型企业网站模板,为五金机械设备类企业打造,通过标准化数字展示提升客户转化率。模板采用模块化设计,可快速适配机床工具、建筑五金、阀门管件等细分领域。查看源码
  • html5响应式pbootcms模板新闻资讯博客网站源码该模板采用PbootCMS内核开发,专为新闻资讯类网站打造,同时具备高度行业适配性--只需替换图文内容即可快速转型为企业官网、行业门户等各类站点。查看源码
  • (PC+WAP)家居装饰家装建材营销型网站pbootcms模板下载本模板基于PbootCMS开发,主要服务于家居装饰、建材及相关行业。采用现代化设计风格,同时适配PC和移动设备访问。模板结构清晰,突出展示家装案例和建材产品,帮助企业在线上建立专业形象,有效展示产品与服务优势。查看源码
分享笔记 (共有 篇笔记)
验证码: