首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用自定义节覆盖模板

使用自定义节覆盖模板
EN

Stack Overflow用户
提问于 2015-09-30 13:32:08
回答 1查看 550关注 0票数 17

我正在尝试覆盖我的自定义部分的默认模板,我正在使用代码来做到这一点,但如果我正在使用它,我无法分配一个模板到编辑页面,谁能告诉我如何在自定义部分和编辑页面分配模板工作。我想在创建页面时设置模板,并在分配它之后想要覆盖它。假设我有一个博客页面,我想给它分配archive.php模板,10个人想从定制部分覆盖它。有一个特定的条件,我想让它工作。

代码语言:javascript
复制
 <?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 ); 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-16 08:44:15

  1. 如何从post编辑屏幕中选择模板框。

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

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

代码语言:javascript
复制
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中的模板文件并提取这个值。因此,如果它始终遵循相同的逻辑(稍微优化了流程),您可以在保存后对其进行更改。这是在编辑后屏幕上显示的文件,并且将始终提取DB值,除非wp尝试加载模板并意识到它不再存在,在这种情况下,它将恢复为选择框中的默认文件。

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

您在此处使用include是不正确的。不要忘记筛选器希望返回的值能够正常工作,在本例中为$template

因此,如果我们想要更改页面的模板...

代码语言:javascript
复制
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. 设置选择

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

虽然您没有为选择框设置默认值,但如果没有标记为选中,您的页面将呈现select的第一个值,因此它的工作方式应该是相同的。

代码语言:javascript
复制
$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)
 ));

如果你重新保存上面的所有选项,它应该是有效的(这是为我)!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32858489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档