通过插件创建 WordPress 自定义页面模板

知更鸟
WordPress 2 879 阅读模式

为页面选择不同页面模板是 WordPress 的一项基本功能。如果您是插件开发人员,准备为插件使用专用的模板,在不修改主题的情况下,很难为网站添加新的页面模板。可以通过下载的代码,利用插件为网站添加新的页面模板,而无需修改主题。

文章源自知更鸟-https://zmingcx.com/create-custom-page-templates-with-plugins.html

通过插件创建 WordPress 自定义页面模板

通过插件创建 WordPress 自定义页面模板

文章源自知更鸟-https://zmingcx.com/create-custom-page-templates-with-plugins.html

将下面的代码添加到插件中: 文章源自知更鸟-https://zmingcx.com/create-custom-page-templates-with-plugins.html

add_filter( 'page_template', 'zm_page_template' );
// 加载页面模板
function zm_page_template( $page_template ) {
	if ( get_page_template_slug() == 'zm-template.php' ) {
		$page_template = dirname( __FILE__ ) . '/templates/zm-template.php';
	return $page_template;