Create a ‘single’ page for a Custom Post Type

Ok, I installed the Custom Post Type UI plugin and create one. I then added a new post to it. In my theme, i have a piece of code like this:

<?php 
    $loop = new WP_Query( array( 
        'post_type' => 'case studies',   /* edit this line */
        'posts_per_page' => 15 ) );
?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>  

    <a href="https://wordpress.stackexchange.com/questions/50372/<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        <?php the_post_thumbnail('thumbnail'); ?>
    </a>

<?php endwhile; ?> 

Now, firstly if i click the thumbnail, i get an error in the browser saying it’s in a redirect loop, but secondly I’d like to know exactly what files i need to create to view a single post of this custom post type. And what to put in that file.

4 s
4

Use single-{posttype}.php for the single template. Also, if you register your post type with the has_archive argument set to true, then you can use archive-{posttype}.php for your archive template, which will allow you to skip that query that you have there, since the global $wp_query object will already be populated with your custom post type.

BTW, you have a space in your post_type argument, which will be a problem.

Check out the Template Hierarchy, and consider registering your CPTs using code in a plugin rather than using a CPT UI plugin.

Leave a Comment