我想直接从插件向主题添加页面模板。其想法是模板将显示在页面属性下的下拉列表中,所有代码都需要在插件中。
关于如何做到这一点,有什么建议吗?
我想直接从插件向主题添加页面模板。其想法是模板将显示在页面属性下的下拉列表中,所有代码都需要在插件中。
关于如何做到这一点,有什么建议吗?
您可以使用theme_page_templates
筛选以将模板添加到页面模板下拉列表中,如下所示:
function wpse255804_add_page_template ($templates) {
$templates[\'my-custom-template.php\'] = \'My Template\';
return $templates;
}
add_filter (\'theme_page_templates\', \'wpse255804_add_page_template\');
现在WP将搜索my-custom-template.php
在主题目录中,因此您必须使用page_template
过滤方式如下:function wpse255804_redirect_page_template ($template) {
if (\'my-custom-template.php\' == basename ($template))
$template = WP_PLUGIN_DIR . \'/mypluginname/my-custom-template.php\';
return $template;
}
add_filter (\'page_template\', \'wpse255804_redirect_page_template\');
请在此处阅读更多信息:Add custom template page programmatically这是以上答案和以上评论的组合,最终对我有效。
将插件添加到可用模板列表的功能:
function wpse255804_add_page_template ($templates) {
$templates[\'my-custom-template.php\'] = \'My Template\';
return $templates;
}
add_filter (\'theme_page_templates\', \'wpse255804_add_page_template\');
将模板指向插件中相应目录的函数:function wpse255804_redirect_page_template ($template) {
$post = get_post();
$page_template = get_post_meta( $post->ID, \'_wp_page_template\', true );
if (\'my-custom-template.php\' == basename ($page_template))
$template = WP_PLUGIN_DIR . \'/mypluginname/my-custom-template.php\';
return $template;
}
add_filter (\'page_template\', \'wpse255804_redirect_page_template\');
谢谢你的帮助。为了使用当前的WordPress版本,我稍微修改了代码。还将其更改为支持多个自定义模板。
我打赌有更好的方法可以做到这一点,但它对我很有效。
/**
* Load Template with Plugin
*/
function yourname_add_page_template ($templates) {
$templates[\'page-one.php\'] = \'title here One\';
$templates[\'page-two.php\'] = \'title here Two\';
$templates[\'page-three.php\'] = \'title here Three\';
return $templates;
}
add_filter (\'theme_page_templates\', \'yourname_add_page_template\');
function yourname_redirect_page_template ($template) {
$post = get_post();
$page_template = get_post_meta( $post->ID, \'_wp_page_template\', true );
if (\'page-one.php\' == basename ($page_template)) {
$template = WP_PLUGIN_DIR . \'/pluginname/templates/page-one.php\';
return $template;
}
elseif (\'page-two.php\' == basename ($page_template)) {
$template = WP_PLUGIN_DIR . \'/pluginname/templates/page-two.php\';
return $template;
}
elseif (\'page-three.php\' == basename ($page_template)) {
$template = WP_PLUGIN_DIR . \'/pluginname/templates/page-three.php\';
return $template;
}
}
add_filter (\'page_template\', \'yourname_redirect_page_template\');
抄本:
<?php
$templates = get_page_templates();
foreach ( $templates as $template_name => $template_filename ) {
echo "$template_name ($template_filename)<br />";
}
?>
然后使用当前模板,并以编程方式将其添加到所需的内容中。下面的代码片段是经过深思熟虑的,它将尝试在插件中查找模板,如果在那里找不到模板,它将尝试从主题中获取模板。
define( \'MY_PLUGIN_DIR\', plugin_dir_path( __FILE __ ) );
define( \'MY_PLUGIN_TEMPLATE_DIR\', MY_PLUGIN_DIR . \'/templates/\' );
add_filter( \'template_include\', \'ibenic_include_from_plugin\', 99 );
function ibenic_include_from_plugin( $template ) {
$new_template = \'\';
$provided_template_array = explode( \'/\', $template );
// This will give us archive.php
$new_template = end( $provided_template_array );
// Define single and archive template for custom post type \'portfolio\'
if( is_singular(\'portfolio\') ) {
$new_template = \'single-portfolio.php\';
}
if( is_post_type_archive( \'portfolio\' ) ) {
$new_template = \'archive-portfolio.php\';
}
$plugin_template = MY_PLUGIN_TEMPLATE_DIR . $new_template;
if( file_exists( $plugin_template ) ) {
return $plugin_template;
}
return $template;
}
资料来源:https://www.ibenic.com/include-or-override-wordpress-templates/<?php load_template( $_template_file, $require_once ) ?>
那是从very 1st 谷歌搜索“从插件加载模板”的结果。在这里提问之前,请自己努力找到答案。我有主题,我添加了WooCommerce插件。但我想更改默认模板(添加侧栏,更改html…)。我可以将woocommerce模板添加到我的主题并进行编辑吗?例如my theme folder -woocommerce --templates ---single-product.php