gpt4 book ai didi

wordpress - 使用自定义部分覆盖模板

转载 作者:行者123 更新时间:2023-12-03 14:22:03 25 4
gpt4 key购买 nike

我试图覆盖自定义部分中的默认模板,我正在使用代码来执行此操作,但是如果使用它,则无法将模板分配给编辑页面页面,谁能给出一个想法,自定义部分如何和编辑页面分配模板工作。我要在创建页面时设置模板,并在分配页面后覆盖。
考虑到我有一个博客页面,我想为其分配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 );

最佳答案

选择模板框如何在后期编辑屏幕中工作。


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

因此,更改此值的一种方法是在保存帖子后对其进行更改。

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引用模板文件并提取该值。因此,如果它始终遵循相同的逻辑(可以稍微优化该过程),则可以在保存后对其进行更改。这是显示在编辑帖子屏幕中的文件,除非wp尝试加载模板并且意识到它不再存在,否则它将始终拉出db值,在这种情况下,它将恢复为选择框中的默认文件。


过滤器 template_include在为页面帖子类型搜索正确模板的功能内(其他帖子类型具有过滤器 single_template


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

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

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;
}



设置选择


您缺少默认值,所以我建议使用以下mod,因为我看不到 $template中的设置,但是如果文件名正确,则用它替换 get_option('current_page_template')

当您没有为选择框设置默认值时,如果没有标记为选中状态,则页面将呈现选择的第一个值,因此它应该起作用。

$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)
));


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

关于wordpress - 使用自定义部分覆盖模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32858489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com