1

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.

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?

13
  • Try naming the cpt and the file without the underscore, I think I remember having to do that once. Just sdpstar or something. Mar 7, 2014 at 22:19
  • Are you using any caching plugins? Eg, W3 Total Cache?
    – vancoder
    Mar 7, 2014 at 22:24
  • @vancoder Good question, but no I'm not running any cache plugins. Mar 7, 2014 at 22:26
  • @AndrewBartel I tried renaming the post to single-sdpstar and the file sdpstar.php and the problem is not resolved. Mar 7, 2014 at 22:33
  • Hmm, your code is sound, and works for me once permalinks are flushed. I would copy and paste your code into the functions file of a standard theme (IE 2014), deactivate all plugins, and see if the issue persists.
    – vancoder
    Mar 7, 2014 at 22:35

1 Answer 1

0

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' );

    ?>
6
  • I don't like the idea of overriding the template engine this way, but I'm running out of time to solve this problem and I want to try it. However, this code is not working for me. Is it working as-is for you? Mar 10, 2014 at 18:06
  • Stupid mistake, sorry, I will update my answer. get_stylesheet_directory should be get_template_part. I'm using a child theme. Mar 10, 2014 at 18:28
  • Let me know if that doesn't work Mar 10, 2014 at 18:30
  • 1
  • Thanks for that Milo. Haven't thought of that. +1 for that Mar 10, 2014 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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