Single page template for custom post_type

I registered a custom post type with the following code:

#Register custom post type for Stars of the Month
add_action( 'init', 'sdp_create_star_type' );
function sdp_create_star_type() {

    register_post_type( 'sdp_star',
        array(
            'labels' => array(
                'name' => __( 'Stars' ),
                'singular_name' => __( 'Star' ),
                'add_new_item' => 'Add New Star of the Month',
                'edit_item' => 'Edit this Star',
                'all_items' => 'All Stars'
            ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'stars'),
        'menu_icon' => 'dashicons-star-filled',
        'menu_position' => 5, //just below posts
        'supports' => array('title', 'thumbnail')
        )
    );
    //flush_rewrite_rules(); //<--This doesn't work either
}

I named the template my_theme/single-sdp_star.php as per the WP naming conventions.

Read More

PROBLEM: No matter what I do, the wrong template file gets used: my_theme/single.php.

I already tried re-saved the permalinks (many times) and this does not help.

What else could be I be missing?

Related posts

1 comment

  1. I had the same problem. Why WordPress doesn’t automatically use the custom single.php template is above me. That aside, here is the code I’m using to force my CPT to use my custom single.php. Just change the CPT and template name to suite your needs.

        <?php
       /* Information Posts Template selection - a single.php just for our information posts */
    function pietergoosen_info_template_include( $original_template ) {
        if ( isset( $wp->query_vars['information'] ) && false == $wp->query_vars['information']  ) {
            return get_template_directory() . '/single-information.php';
        } else {
            return $original_template;
        }
    }
    
    add_filter( 'template_include', 'pietergoosen_info_template_include' );
    
        ?>
    

Comments are closed.