使用自定义部分覆盖模板

Template Overriding using a customization section

我正在尝试从自定义部分覆盖我的默认模板,我正在使用代码来做到这一点,但是如果我正在使用它,我无法将模板分配给编辑页面,任何人都可以提供一个想法自定义部分和编辑页面分配模板如何工作。我想在创建页面时设置模板,并在分配它之后我想覆盖它。 假设我有一个博客页面,我想为它分配 archive.php 模板,十个想从自定义部分覆盖它。我希望它在特定条件下工作。

 <?php
    /**
     * Adds the Customize page to Select template For Pages
     */

    add_action( 'wp_footer', 'cur_page_template' );
function cur_page_template(){
    var_dump( get_option('current_page_template') );
    var_dump( get_page_template() );
    exit;
}
 function widgetsite_template_override($wp_customize){
    $wp_customize->add_panel( 'template_options', array(
        'title' => __( 'Template Options', 'widgetsite' ),
        'description' => $description, // Include html tags such as <p>.
        'priority' => 160, // Mixed with top-level-section hierarchy.
    ) );

    $wp_customize->add_section('theme_template_override', array(
        'title'    => __('Override  Templates', 'widgetsite'),
        'panel' => 'template_options',
        'description' => '',
        'priority' => 120,
    ));


    $templates = get_page_templates();

    $cats = array();
    $i = 0;
    foreach($templates as $template_name => $template_file){
        //$cats[$template_name] = $template_name;   
        if (strpos($template_file,'layouts') !== false) {
            $cats[$template_file] = $template_name;
        }
    }


    $wp_customize->add_setting('widgetsite_archive_template');
    $wp_customize->add_setting('widgetsite_page_template');
    $wp_customize->add_setting('widgetsite_index_template');
    $wp_customize->add_setting('widgetsite_post_template');
    $wp_customize->add_setting('widgetsite_search_template');

    $wp_customize->add_control( 'widgetsite_archive_template', array(
        'settings' => 'widgetsite_archive_template',
        'label'   => 'Override Archive Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "archive.php"=>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_page_template', array(
        'settings' => 'widgetsite_page_template',
        'label'   => 'Override Page Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge( array( "page.php" =>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_index_template', array(
        'settings' => 'widgetsite_index_template',
        'label'   => 'Override Index Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "index.php"=>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_post_template', array(
        'settings' => 'widgetsite_post_template',
        'label'   => 'Override Post Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "post.php"=>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_search_template', array(
        'settings' => 'widgetsite_search_template',
        'label'   => 'Override Search Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "search.php"=>get_option('current_page_template')), $cats)
    ));

}
add_action('customize_register', 'widgetsite_template_override');

$theme_mode_templates['archive.php'] = get_theme_mod("widgetsite_archive_template");
$theme_mode_templates['page.php'] = get_theme_mod("widgetsite_page_template");
$theme_mode_templates['index.php'] = get_theme_mod("widgetsite_index_template");
$theme_mode_templates['post.php'] = get_theme_mod("widgetsite_post_template");
$theme_mode_templates['search.php'] = get_theme_mod("widgetsite_search_template");


function widgetsite_template_redirect($template){

    global $wp_query;
    global $post;
    $cur= basename($template);
    if(  $cur === 'page.php' && get_theme_mod("widgetsite_page_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_page_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'archive.php' && get_theme_mod("widgetsite_archive_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_archive_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'index.php' && get_theme_mod("widgetsite_index_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_index_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'post.php' && get_theme_mod("widgetsite_post_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_post_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'search.php' && get_theme_mod("widgetsite_search_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_search_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    return $template;


}
add_filter( 'template_include', 'widgetsite_template_redirect', 99 ); 
  1. post 编辑屏幕中选择模板框的工作原理。

重要的是要记住页面也是 post 并且所有与 post 相关的元都存储在 post 元 table 中。页面 post 类型与标准 post 类型略有不同,因为它们不遵循 single-postname.php 模板使用功能。相反,页面将模板文件路径保存在 wp_postmeta 数据库 table 中,关键字为 _wp_page_template

因此更改此值的一种选择是在保存后更改它 post。

function save_template_file( $post_id ) {

    if ( 'page' != $post->post_type ) {
        return;
    }

    //insert logic here
    $filelocation= 'anywhere.....';

    update_post_meta($post_id, '_wp_page_template', $filelocation);

}

add_action('save_post', 'save_template_file', 11 );

现在这不是你要找的,但你提到你想了解这个过程,所以对于页面,wp 将引用来自 post meta 的模板文件并提取这个值。因此,如果它始终遵循相同的逻辑(稍微优化了流程),您可以在保存后更改它。这是显示在编辑 post 屏幕中的文件,它将始终拉取 db 值,除非 wp 尝试加载模板并意识到它不再存在,在这种情况下,它将恢复为 select箱子。

  1. 过滤器 template_include 位于为页面 post 类型搜索正确模板的函数中(其他 post 类型具有过滤器 single_template

您在此处使用的 include 不正确。不要忘记,在这种情况下,过滤器会期望返回的值能够正常工作 $template

所以如果我们想更改页面模板....

add_filter('template_include', 'assign_new_template');

function assign_new_template ($template){

 //we already have a template name, no need to pull it again..
 $cur= basename($template);

 if(  $cur === 'page.php' && get_theme_mod("widgetsite_page_template")){  //note $cur will never be empty!
    $template=  get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_page_template");// assuming this will return correct template...
    //if issues try hardcoding a path to test...
 }

// dont need a else, we will only change the template if our logic is satisfied...

// etc

return $template;
}
  1. 设置 selects

您缺少默认值,因此我建议使用以下 mod,因为我看不到您在 get_option('current_page_template') 中的设置,但如果文件名正确,请将 page.php 替换为它。

虽然您没有为 select 框设置默认值,但如果 none 被标记为 selected,您的页面将呈现 select 的第一个值所以它应该是一样的。

$wp_customize->add_control( 'widgetsite_search_template', array(
    'settings' => 'widgetsite_search_template',
    'label'   => 'Override Search Template:',
    'section'  => 'theme_template_override',
    'type'    => 'select',
    'choices' => array_merge(array("page.php"=>'default'), $cats)
 ));

如果您像上面那样重新保存所有选项,它应该可以工作(对我来说)!