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'm trying to make a custom template for my category pages that only pulls in post under a specific category. At the moment, I am only able to pull in all post and not category specific...

My code so far....

<?php
    //Identify current Post-Category-ID
    foreach((get_the_category()) as $category)
    {
        $postcat= $category->cat_ID;
        $catname =$category->cat_name;
    }
?>
//Print category ID
<h2><?php echo $catname; ?></h2>
<?php 
$thumbnails = get_posts();
foreach ($thumbnails as $thumbnail) {
    if ( has_post_thumbnail($thumbnail->ID)) {
      echo '<li><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
      echo get_the_post_thumbnail($thumbnail->ID, 'full');
      echo '</a></li>';
    }
}
?>
share|improve this question
2  
Currently your question is a "do this job for me"-question. Please show us your research effort: What do you think would be usable, what have you already tried and why did it fail, etc. –  kaiser Sep 24 '12 at 12:40
add comment

4 Answers

up vote 1 down vote accepted
+50

You've already got code to figure out which category you want to show posts from, here is how you would grab all the posts in that category:

// create a query to grab our posts in category of ID $postcat
$q = new WP_Query(array( 'cat' => $postcat));
if($q->have_posts()){
    // foreach post found
    while($q->have_posts()){
        $q->the_post();
        // code for displaying each post goes here
    }
    // cleanup after the WP_Query, reset the post data
    wp_reset_postdata();
} else {
    // no posts were found!
}

Never use query_posts to do your queries, always check if any posts were actually found, and always cleanup after yourself.

For more arguements for queries, see here:

http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
add comment

there is another way easier than that all in archive.php

replace that code

<?php
get_template_part('loop', $the_template_part);}
    ?> 

with this code

<?php
    if (is_category('CAT_ID'))
        {get_template_part('loop2', $the_template_part);
        } 
         else {get_template_part('loop', $the_template_part);}
    ?> 

and then make your custom loop as you want and name it loop2 or what ever that will give you what you want exactly , showing just the category posts in the custom loop that you want to make

share|improve this answer
add comment

I'm trying to make a custom template for my category pages that only pulls in post under a specific category.

You know that we have category templates built in to WordPress.

http://codex.wordpress.org/Category_Templates

You really can just make category-slug.php for the category and be done with it. Unless you're trying to say that you want the category page to show that category, and none of the posts that may be in children or other categories.

share|improve this answer
add comment

using the following code you can fetch all the post of this category.If you want to limit the number of post then you have to pass the number to show post argument

 <?php query_posts('cat=1&showposts=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php the_excerpt() ?>
  </li>
<?php endwhile; endif; ?>
share|improve this answer
    
is that specific to cat id 1 only? I'm trying to make a template that only prints post under a category. –  calebo Sep 24 '12 at 13:49
2  
Do not use query_posts –  Tom J Nowell Sep 27 '12 at 13:22
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.