zoukankan      html  css  js  c++  java
  • 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”页面所调用,使用的语句是:

    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中。他们的关系如下图:
    1
     

    在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( 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 = '';
    			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就自动会把这个模板识别成页面模板。下图为获取模板值部分的关系图:

    2

    Tips

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

  • 相关阅读:
    Elasticsearch 删除文档
    Elasticsearch 更新文档
    Elasticsearch 修改数据
    Elasticsearch 使用集群
    Elasticsearch 使用集群
    Elasticsearch 使用集群
    Elasticsearch 使用集群
    Elasticsearch 集群
    Elasticsearch 使用集群
    Elasticsearch 安装
  • 原文地址:https://www.cnblogs.com/TransTown/p/2034212.html
Copyright © 2011-2022 走看看