WordPress代码和分析-页面模板是从哪里来的(这是一个可以让人奔溃的设计)

今天看到WordPress中的可以自定义页面模板,很好奇在添加页面时,这些页面模板的选项从哪里来的?

未命名

首先找到Post.php,竟然没有找到这区域是在post.php的那个函数内输出的,后来通过查找输出页面Html代码,找到了输出位置是在wp-admin/includes/meta-boxes.php的“page_attributes_meta_box”方法中。

page_attributes_meta_box方法就是用来显示页面属性,它被“edit-form-advanced.php”页面所调用,使用的语句是:

1
add_meta_box( 'pageparentdiv' , 'page'  == $post_type  ? __( 'Page Attributes' ) : __( 'Attributes' ), 'page_attributes_meta_box' , $post_type , 'side' , 'core' );
1
通过add_meta_box来调用了“page_attributes_meta_box”。
1
meta-boxes.php是一个插件,是在<span style= "font-family: 宋体;"  face= "宋体" >”wp-admin\edit-form-advanced.php”通过 require_once ( './includes/meta-boxes.php' );引入到程序中的。而”wp-admin\edit-from-advanced.php”也是通过</span>
1
require_once 被包含在post.php中。他们的关系如下图:
1
<a href= "http://images.cnblogs.com/cnblogs_com/TransTown/201105/20110502014502409.jpg" ><img height= "92"  width= "689"  src= "http://images.cnblogs.com/cnblogs_com/TransTown/201105/201105020145026788.jpg"  alt= "1"  border= "0"  title= "1"  style= "background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" ></a>
1
  

在meta-boxes.php中,使用了page_template_dropdown方法来输出select的内容。

1
2
3
4
<select name= "page_template"  id= "page_template" >
<option value= 'default' ><?php _e( 'Default Template' ); ?></option>
<?php page_template_dropdown( $template ); ?>
</select>
1
在文件”wp-admin/includes/template.php "中找到了这个方法,而在wp-admin\includes\admin.php" : require_once (ABSPATH . 'wp-admin/includes/template.php' );中引入了
1
template.php这个文件到wordpress中。
1
在template.php中,page_template_dropdown方法主要是通过”get_page_templates“方法获取到option的值。get_page_templates是来自于”wp-admin/includes/theme.php”文件的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function  get_page_templates() {
     <span style= "color: #ff0000;"  color= "#ff0000" > $themes  = get_themes();
     $theme  = get_current_theme();</span>
     $templates  = $themes [ $theme ][ 'Template Files' ];
     $page_templates  = array ();
     if  ( is_array ( $templates  ) ) {
         $base  = array ( trailingslashit(get_template_directory()), trailingslashit(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  = '' ;
             <span style= "color: #0000ff;"  color= "#0000ff" > if  ( preg_match( '|Template Name:(.*)$|mi' , $template_data , $name  ) )
                 $name  = _cleanup_header_comment( $name [1]);</span>
             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就自动会把这个模板识别成页面模板。下图为获取模板值部分的关系图:

2

Tips

可以通过在wp-config.php中把define('WP_DEBUG', false);设置为true,就可以通过print_r和die来逐步输出我们需要查看的内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值