0

Can I add the archive and single page for a custom post type added by my plugin?

I know it will only have to be a best guess with contents like:

<?php get_header(); ?>
<h2><?php _e('My Post Type', 'my-slug'); ?></h2>

<?php //get posts and list ?>

<?php get_footer(); ?>

Which would mean it would need to be possible for a user (or me) to override those pages in a theme using archive-{post-type}.php and single-{post-type}.php or something similar.

1

You can't without hooking a filter to recognise the files in your plugin BUT you can hook into the template_include filter and register your own file e.g.

add_filter('template_include', 'my_function_name');
function my_function_name( $template ) {
 if( is_post_type_archive( 'post_type' ) ){
  $template = dirname( __FILE__ ) . '/templates/archive-post_type.php';
 }
 if( is_singular( 'post_type' ) ){
  $template = dirname( __FILE__ ) . '/templates/single-post_type.php';
 }
 return $template;
}

I was given this code by another WSE user when I had this exact need

  • Thanks! I added ` && !(strpos($template, 'edpn_newsletter'))` into both if statements so that if the theme is providing a custom template for the post type it will respected. – Arcath Jun 22 '16 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.