WordPress代码和分析-页面模板是从哪里来的

来源:互联网 发布:淘宝服务市场 第三方 编辑:程序博客网 时间:2024/04/30 12:50

最近开始接触wordpress。WordPress代码和分析-页面模板是从哪里来的?这个问题在网上找了比较详细的说明。

转文如下:http://www.cnblogs.com/TransTown/archive/2011/05/02/2034212.html


今天看到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 src="http://images.cnblogs.com/cnblogs_com/TransTown/201105/201105020145026788.jpg"alt="1" title="1" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;"border="0" height="92" width="689"></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来逐步输出我们需要查看的内容。