从 WordPress 中的插件加载模板
Loading Templates from a plugin in WordPress
我正在尝试制作我的第一个插件,但遇到了困难。这是想法。
当我的插件被激活时,它将创建一个 post 类型和该 post 类型的两个分类法(在我的例子中,post 类型名称是 'Ads')。
此外,我创建了两个模板页面,一个用于显示所有广告 post 类型文章的列表,另一个用于显示相同 post 类型的单个页面。
现在我的问题是如何告诉 WordPress 在插件处于活动状态时从插件文件夹而不是主题文件夹中查找模板。?
这是我可以在插件文件中做的事情还是我必须为此创建另一个文件?
这应该符合您的要求:
首先,这个钩子告诉 WordPress 哪个是您插件中的单个 CPT 模板
从这个答案中,您可以获得 single_template 挂钩以及如何加载它。
定义一个常量来替换“plugin_dir_path( FILE )”,如果你在插件的其他地方使用它,像这样:
define('YOUR_PLUGIN_DIR_PATH', trailingslashit(plugin_dir_path( __FILE__ )) );
https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
function load_single_ad_template( $template ) {
global $post;
if ( 'ads' === $post->post_type && locate_template( ['single-ads.php'] ) !== $template ) {
/*
* This is an 'ads' post
* AND a 'single ad template' is not found on
* theme or child theme directories, so load it
* from our plugin directory from inside a /templates folder.
*/
return YOUR_PLUGIN_DIR_PATH . 'templates/single-ads.php';
}
return $template;
}
add_filter( 'single_template', 'load_single_ad_template', 10, 1 );
然后对于广告存档模板,'archive_template' 挂钩,如下所示:
function load_archive_ads_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'ads' ) ) {
$archive_template = YOUR_PLUGIN_DIR_PATH . 'templates/archive-ads.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'load_archive_ads_template', 10, 1 ) ;
官方文档:
https://developer.wordpress.org/reference/hooks/type_template/
https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
这未经测试,但应该可以,但是请告诉我。
我正在尝试制作我的第一个插件,但遇到了困难。这是想法。 当我的插件被激活时,它将创建一个 post 类型和该 post 类型的两个分类法(在我的例子中,post 类型名称是 'Ads')。 此外,我创建了两个模板页面,一个用于显示所有广告 post 类型文章的列表,另一个用于显示相同 post 类型的单个页面。
现在我的问题是如何告诉 WordPress 在插件处于活动状态时从插件文件夹而不是主题文件夹中查找模板。? 这是我可以在插件文件中做的事情还是我必须为此创建另一个文件?
这应该符合您的要求:
首先,这个钩子告诉 WordPress 哪个是您插件中的单个 CPT 模板
从这个答案中,您可以获得 single_template 挂钩以及如何加载它。
定义一个常量来替换“plugin_dir_path( FILE )”,如果你在插件的其他地方使用它,像这样:
define('YOUR_PLUGIN_DIR_PATH', trailingslashit(plugin_dir_path( __FILE__ )) );
https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
function load_single_ad_template( $template ) {
global $post;
if ( 'ads' === $post->post_type && locate_template( ['single-ads.php'] ) !== $template ) {
/*
* This is an 'ads' post
* AND a 'single ad template' is not found on
* theme or child theme directories, so load it
* from our plugin directory from inside a /templates folder.
*/
return YOUR_PLUGIN_DIR_PATH . 'templates/single-ads.php';
}
return $template;
}
add_filter( 'single_template', 'load_single_ad_template', 10, 1 );
然后对于广告存档模板,'archive_template' 挂钩,如下所示:
function load_archive_ads_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'ads' ) ) {
$archive_template = YOUR_PLUGIN_DIR_PATH . 'templates/archive-ads.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'load_archive_ads_template', 10, 1 ) ;
官方文档:
https://developer.wordpress.org/reference/hooks/type_template/ https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
这未经测试,但应该可以,但是请告诉我。