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.

I am wanting to create a page template file to display posts from a custom content type. I was working with this code I found from this site, but it renders nothing. Does anyone know what the problem is?

<?php /* Template Name: Custom Post Type Archive */
get_header(); ?>

<?php
global $query_string;
query_posts($query_string . "post_type=YOUR-CUSTOM-POST-TYPE&post_status=publish&posts_per_page=10");
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endwhile;
endif; ?>
<div class="navigation">
    <div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>

<?php get_sidebar(); ?>
<?php get_footer();?>
share|improve this question

2 Answers 2

up vote 4 down vote accepted

You don't need to use a query, just create your tample and name single-YOURCUSTOMPOSTNAME.php, for more references take a look at:

share|improve this answer
    
I'm not sure I'm following along. Could you explain your answer a bit more? –  Keith Groben Feb 3 '12 at 8:13
    
the default page template for a custom post type, is single-NAME.php, so let's say your custom post type is calle "Sports", so you need to create that theme and save it as single-sports.php, you have your theme already created, only have to erase that query at line 5, and leave the loop alone. Also notice the next and prev links are not going to work since those functions don't work with CPT, however there are some plugins for that around, google is your friend. –  andresmijares25 Feb 3 '12 at 8:20

If you have created a new Custom Post Type then to show them on the site you need to use the following files in your theme folder:

  • archive-{CPT}.php
  • single-{CPT}.php (Optional if you want to display a single post differently)

Replace {CPT} with the name of the Custom Post Type you have setup. Then in those files just do a normal loop like in your index.php file.

E.G.

<?php get_header(); ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <?php /* Your Posts Content */ ?>

    <?php endwhile; else: ?>
        <div class="post">
            <p><?php _e('Sorry, no posts matched your criteria.', "hi-rezz"); ?></p>
        </div>
    <?php endif; ?>

<?php get_footer(); ?>
share|improve this answer
    
Thank you for a detailed explanation. Please bear with me as I am relatively new to this type of modifications in WordPress. My CPT is "New Movies". The slug for this is "new_movies". the archive file is "archive-new_movies.php". Then I put the loop code in the archive file? –  Keith Groben Feb 3 '12 at 8:54
    
That would be correct –  Brady Feb 3 '12 at 9:04
    
I added the loop code and all I am seeing is a permalink to the page. So, I copied the default archive page and still received the same result. Do you have any ideas why this may be happening? –  Keith Groben Feb 3 '12 at 9:43

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.