为页面选择不同页面模板是 WordPress 的一项基本功能。如果您是插件开发人员,准备为插件使用专用的模板,在不修改主题的情况下,很难为网站添加新的页面模板。可以通过下载的代码,利用插件为网站添加新的页面模板,而无需修改主题。
将下面的代码添加到插件中:
add_filter( 'page_template', 'page_template' );
// 加载页面模板
function page_template( $page_template ) {
if ( get_page_template_slug() == 'template.php' ) {
$page_template = dirname( __FILE__ ) . '/templates/template.php';
}
return $page_template;
}
// 添加到页面属性模板中
add_filter( 'theme_page_templates', 'add_template_select', 10, 4 );
function add_template_select( $post_templates, $wp_theme, $post, $post_type ) {
$post_templates['template'] = __( '模板名称' );
return $post_templates;
}
其中:
模板文件:template.php
模板文件路径:/templates/template.php
显示在页面属性中的模板名称:$post_templates[‘template’] = __( ‘模板名称’ );
糖果博客,原文地址https://wpocn.com/4340.html© 版权声明
版权声明
1
本站名称:糖果博客
2
本站网址:wpocn.com
3
本网站的内容来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4
本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5
本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报。
6
本站附件资源、教程等内容如因时效原因失效或不可用,请评论区留言或联系站长及时更新。
THE END













暂无评论内容