0

The following function enables a single custom post type template from a plugin:

function ch4_br_template_include( $template_path ) {
           if ( get_post_type() == 'book_reviews' ) {
               if ( is_single() ) {
                   // checks if the file exists in the theme first,
                   // otherwise serve the file from the plugin
                   if ( $theme_file = locate_template( array
                       ( 'single-book_reviews.php' ) ) ) {
                       $template_path = $theme_file;
                   } else {
                       $template_path = plugin_dir_path( __FILE__ ) .
                           '/single-book_reviews.php';
} }
}
           return $template_path;
       }

What is the equivalent for displaying a list of my custom post types? I want to have a separate template for displaying a table of, in my case 'resellers', at mysite/resellers.

Here is my custom post type:

public function wps_reseller_cpt() {
        $labels = array(
            'name'               => _x( 'WPS Resellers', 'post type general name' ),
            'singular_name'      => _x( 'WPS Reseller', 'post type singular name' ),
            'add_new'            => _x( 'Add New', 'book' ),
            'add_new_item'       => __( 'Add New Reseller' ),
            'edit_item'          => __( 'Edit Reseller' ),
            'new_item'           => __( 'New Reseller' ),
            'all_items'          => __( 'All Resellers' ),
            'view_item'          => __( 'View Reseller' ),
            'search_items'       => __( 'Search Resellers' ),
            'not_found'          => __( 'No resellers found' ),
            'not_found_in_trash' => __( 'No Reseller found in the Trash' ),
            'parent_item_colon'  => '',
            'menu_name'          => 'Resellers'
        );
        $args = array(
            'labels'        => $labels,
            'description'   => 'Holds our resellers and reseller specific data',
            'public'        => true,
            'menu_position' => 50,
            'supports'      => array( 'title', 'editor','image', 'thumbnail', 'custom-fields' ),
            'rewrite' => array('slug' => 'resellers'),
            'has_archive'   => true,
        );
        register_post_type( 'wps-reseller', $args );
    }
1

Instead of using the 'single_template' filter use the more generic 'template_include' filter for both archive annd single post view:

add_filter('template_include', 'my_custom_templates');

function my_custom_templates( $template ) {
  if ( is_single() && ( 'book_reviews' == get_post_type() ) ) {
    if ( ! locate_template('single-book_reviews.php', false) )
       $template = plugin_dir_path( __FILE__ ) . '/single-book_reviews.php';
  } elseif( is_post_type_archive('book_reviews') ) {
     if ( ! locate_template('archive-book_reviews.php', false) )
       $template = plugin_dir_path( __FILE__ ) . '/archive-book_reviews.php';
  }
  return $template;
}

After that you have just to create the file 'archive-book_reviews.php' in your plugin folder.

| improve this answer | |

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.