Title: list posts under category
Last modified: August 19, 2016

---

# list posts under category

 *  [2cats](https://wordpress.org/support/users/2cats/)
 * (@2cats)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/list-posts-under-category/)
 * Hi, I am hoping someone can help me with the basic loop code for an archive.php
   page (which would also function as the categories page). Ideally I’d like to 
   just produce a list of post titles, or post titles with brief excerpts. Here’s
   my code — it brings up the first post under the first category, and nothing else:
 *     ```
       <?php
       //get categories, if category has posts, get a post in that category and display it
       $categories=get_categories();
       if ($categories) {
         foreach($categories as $category) {
           if ($category->count > 0) {
             echo '<p>Category <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> has ' . $category->count . ' post(s). </p> ';
             $number_posts_per_category = -1; // set = -1 to see all posts in the category
             $posts=get_posts('cat='. $category->term_id .'&showposts='.$number_posts_per_category);
             foreach($posts as $post) {
               setup_postdata($post); ?>
               <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
               <?php the_content();
             } // foreach ($posts
           } // if ($category->count > 0
         } // foreach ($categories
       } // if ($categories
       ?>
       ```
   
 * I am using permalinks, if that helps. I am still in the learning phase (obviously);
   many thanks in advance for any help offered.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/list-posts-under-category/#post-1403983)
 * Change this:
 *     ```
       <?php the_content();
       ```
   
 * to
 *     ```
       <?php the_excerpt();
       ```
   
 * Also review the first four questions here:
    [http://codex.wordpress.org/FAQ_Layout_and_Design#Text_and_Content_Display](http://codex.wordpress.org/FAQ_Layout_and_Design#Text_and_Content_Display)
 * [edited link]
 * See [Excerpt](http://codex.wordpress.org/Excerpt)
 *  Thread Starter [2cats](https://wordpress.org/support/users/2cats/)
 * (@2cats)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/list-posts-under-category/#post-1404196)
 * Thank you so much, that really helped. Now I have the problem that when I click
   on any category, it brings up all categories alphabetically with the post excerpts
   listed beneath each. How can I limit it strictly to the selected category? I 
   went through the links you recommended (good information) but couldn’t find a
   way to limit it to the category selected by the user. (Lots of information about
   limiting it to a category that I select, though.) Thanks again.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/list-posts-under-category/#post-1404197)
 * You probably need to review [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy)
   because if you coded your [Category Template](http://codex.wordpress.org/Category_Templates)
   to show all categories that could be the problem.
 * Switch to the WordPress Default theme and see how it handles category views.
 * Also, if you are referring to child vs. parent categories that is a different
   issue.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘list posts under category’ is closed to new replies.

 * 3 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/list-posts-under-category/#post-1404197)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
