Wordpress - custom archive page for post type from

2019-07-10 02:17发布

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条回答
聊天终结者
2楼-- · 2019-07-10 02:26

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' ) ;
?>
查看更多
登录 后发表回答