0

I have a custom post type, case-study, defined in my functions.php like so:

function case_study_posttype() {

  register_post_type( 'case-study',
    array(
      'labels' => array(
        'name' => __( 'Case Studies' ),
        'singular_name' => __( 'Case Study' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'case-study'),
      'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
      'menu_position' => 5,
      'menu_icon' => 'dashicons-format-aside',
      'taxonomies' => array('category')
    )
  );
}

add_action( 'init', 'case_study_posttype' );

For this post type I want to use a custom template, so in the root directory of my theme folder I created a file called single-case-study.php, however, when I access the permalink for a single case study, it is not being formatted using my single-case-study.php template. Any ideas?

2

From what you've told us, your single-case-study.php should be the correct file. To help troubleshoot, add this line to your header.php:

<!-- <?php global $template; print_r($template); ?> -->

Then view the source of a single case study and see which template WP is outputting. Once you have identified the file, if the solution isn't clear, you can post an update here for more help.

It's possible you may be cached - may need to clear both your browser cache and your server cache, depending on what plugins or host you're using.

| improve this answer | |
  • Thanks, didn't realize there was a function to show which template is being used! It looks like my single custom post type is using index.php as the template. I tried restarting my MAMP server and viewing in incognito but it is still outputting using index.php. Could this be because of the way I am creating the custom post type? Is there an argument I can add to the custom post type to specify which template to use? – APAD1 May 22 '17 at 19:48
  • 2
    I think you were correct about the caching. I went to Settings>Permalinks and just re-saved the existing settings and it is correctly displaying the custom single template now. Thanks! – APAD1 May 22 '17 at 19:53
  • 1
    Ah, yes, checking the permalinks page (you don't even have to save - just visit the page) is the magic bullet whenever you have CPT issues, from permalinks to templates. Glad you stumbled on the solution. :) – WebElaine May 23 '17 at 13:16

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.