Title: Proper Code to Display Posts from Specific Categories
Last modified: August 20, 2016

---

# Proper Code to Display Posts from Specific Categories

 *  [biggupp](https://wordpress.org/support/users/biggupp/)
 * (@biggupp)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/proper-code-to-display-posts-from-specific-categories/)
 * [http://www.ahy4life.com/update_20121008/](http://www.ahy4life.com/update_20121008/)
 * The above is a testing site. I need to add additional categories but cannot seem
   to fathom precisely the proper code for a specific section on each page. An rtfm
   has been unsuccessful.
    For example, if I wish to display the title and then 
   the content from a post from ‘events’, which is ID=4, in the code below:
 * ‘
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 *  <h2><center><?php the_title(); ?></center></h2>
 * <!– Display the Post’s Content in a div box. –>
 *  <?php the_content(); ?>
 *  <?php endwhile; else: ?>
    <p><?php _e(‘Sorry, no posts matched your criteria.’);?
   ></p> <?php endif; ?> ‘
 * what is the proper code?

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

 *  [Christine Rondeau](https://wordpress.org/support/users/crondeau/)
 * (@crondeau)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/proper-code-to-display-posts-from-specific-categories/#post-3115245)
 * What you could do is use [WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
   and pull the latest post from the category, so something like this:
 *     ```
       <?php
       	$new_query_1 = new WP_Query();
       	$new_query_1->query(array( 'posts_per_page' => 1, 'cat' => 4 ));
       	?>
       	<?php while ($new_query_1->have_posts()) : $new_query_1->the_post(); ?>
       	<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
               <?php the_content() ;?>
       <?php endwhile; ?>
       ```
   
 *  Thread Starter [biggupp](https://wordpress.org/support/users/biggupp/)
 * (@biggupp)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/proper-code-to-display-posts-from-specific-categories/#post-3115287)
 * Can’t get it to quite work. I admit I am a bit of a novice with WP.
    Is there
   not a way to simply specify the category in the existing code?

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

The topic ‘Proper Code to Display Posts from Specific Categories’ is closed to new
replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [biggupp](https://wordpress.org/support/users/biggupp/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/proper-code-to-display-posts-from-specific-categories/#post-3115287)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
