Wordpress 使用自定义节覆盖模板
我正在尝试覆盖自定义部分中的默认模板,我正在使用代码来完成此操作,但是如果我正在使用它,我无法将模板分配给编辑页面,有人能告诉我自定义部分和编辑页面分配模板是如何工作的吗。我希望在创建页面时设置模板,并在分配后覆盖该模板。 考虑到我有一个博客页面,我想把它归档归档.PHP模板,十想从定制部分重写它。我希望它在特定的条件下工作Wordpress 使用自定义节覆盖模板,wordpress,templates,wordpress-theming,Wordpress,Templates,Wordpress Theming,我正在尝试覆盖自定义部分中的默认模板,我正在使用代码来完成此操作,但是如果我正在使用它,我无法将模板分配给编辑页面,有人能告诉我自定义部分和编辑页面分配模板是如何工作的吗。我希望在创建页面时设置模板,并在分配后覆盖该模板。 考虑到我有一个博客页面,我想把它归档归档.PHP模板,十想从定制部分重写它。我希望它在特定的条件下工作 <?php /** * Adds the Customize page to Select template For Pages */
<?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 );
如何从后期编辑屏幕选择模板框李>
重要的是要记住页面也是post,所有与post相关的meta都存储在post meta表中。页面帖子类型与标准帖子类型略有不同,因为它们不遵循single postname.php
template use函数。相反,pages将模板文件路径保存在wp\u postemta
数据库表中,键为\u wp\u page\u 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将引用PostMeta中的模板文件并提取这个值。因此,如果它始终遵循相同的逻辑(稍微优化了流程),则可以在保存后对其进行更改。这是显示在“编辑帖子”屏幕中的文件,除非wp尝试加载模板并意识到模板不再存在,否则将始终拉取db值,在这种情况下,它将恢复为选择框中的默认文件
过滤器template\u include
位于为页面文章类型搜索正确模板的功能中(其他文章类型具有过滤器单个模板
)
您在此处使用的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;
}
设置选项
您缺少默认值,因此我建议使用以下mod,因为我看不到您在get\u选项(“当前页面\u模板”)中的设置。
但是如果有正确的文件名,请用它替换page.php
当您没有为选择框设置默认值时,如果没有标记为“选定”,则页面将呈现选择框的第一个值,因此它的工作方式应相同
$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)
));
如果你像上面那样重新保存所有选项,它应该会工作(这是为我) 谢谢你的回答,但我认为我得到的代码中还有一些其他问题错误警告:include(E:\projects\wamp\www\widgetsite/wp content/themes/widgetsite/layouts/default)
那么,你能找出我从哪里得到这个吗?警告:include(E:\projects\wamp\www\widgetsite/wp content/themes/widgetsite/layouts/default):无法打开流:在第75行的E:\projects\wamp\www\widgetsite\wp includes\template-loader.php中没有这样的文件或目录
这是一个错误,我认为selectbox中的默认模板导致了问题,您能建议我如何处理它吗?该文件是否存在于该位置?它在任何情况下都缺少扩展名,但我使用的文件除外n下拉框中不会缺少该选项。这是您自己制作的一个mod,您是否忘记将扩展放在某个地方?“选项”=>array\u merge(array(“default”=>get\u option('current\u page\u template')),$cats)
这是存在问题的那一行。但是将其更改为甚至index.php
我也会遇到同样的错误。请看我很难调试您创建选择框的方式,它应该保存为“当前页面模板”选项。但是手动将其更改为index.php将在布局中查找index.phpts文件夹(假设您没有更改我给您的代码格式)。您有权访问数据库以检查当前页面模板中的值吗?顺便说一句……您知道每个get\u选项(“当前页面模板”)
将始终保持不变?如果它工作正常,仍然找不到它。在某些情况下它不工作,在某些情况下它工作正常。在哪些情况下它不工作?