I've created a pretty basic plugin which adds a custom post type with a couple taxonomies and a few custom fields. I would rather use a plugin than simply add it to the theme because I think I might be useful in the future. The problem is that I want to customize the archive page for the particular post type. I could obviously just create a new file, in this case archive-research.php but having it in the theme directory would defeat the purpose of using a plugin. Is there a method whereby I could have the custom archive file as part of the plugin itself?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use archive_template
filter, from WordPress codex: https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
<?php
function get_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'my_post_type' ) ) {
$archive_template = dirname( __FILE__ ) . '/post-type-template.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
?>