Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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="<?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.

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

You could just write this into your single.php file (within the loop) and echo out whatever fields you need within the if statement.

if($post_type == 'case_studies') { // you may need this to be without spaces (machine name)

                echo '<h1>'.get_the_title().' flavors</h1>';

               // post id
             $post_id = get_the_ID();
              get_post_meta($post_id, 'custom_field_name', true);


                <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> 
                  <?php endwhile; ?>

 }

Another option is t0 create a page template. Copy your single.php file and rename it case_studies.php .. at the top within php tags add:

<?php
/*
Template Name: Brand Output 04/12
*/
?>

and then add the same if statement within the single.php loop as the above example...

share|improve this answer
    
Thsi works, but it is bad, bad practice, the nearest you should ever get to this is get_template_part('stuff',$post->post_type); –  Tom J Nowell Apr 26 '12 at 22:01
    
can you explain why it is bad practice? –  Starfs Apr 27 '12 at 12:34
    
Because it's unclean code, and you have a tonne of if else statements, and duplicated code. You would be better creating a template file like 'content.php', and doing get_template_part('content',$post_type); and using content-case_studies.php to override it on a per post type basis –  Tom J Nowell Apr 27 '12 at 15:01
    
That way your single.php remains readable. Even then it would eb better ot do it the proper way and use single-case_studies.php –  Tom J Nowell Apr 27 '12 at 15:02
1  
cool. I changed the code in my theme to reflect this new method for outputting custom post types. thanks for the heads up –  Starfs Apr 30 '12 at 18:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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