Title: Code to display posts by category?
Last modified: August 19, 2016

---

# Code to display posts by category?

 *  [chrisb125](https://wordpress.org/support/users/chrisb125/)
 * (@chrisb125)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/)
 * I’m trying to show the post links by each category in my sidebar on my blog.
 * <h2>Category ID 2</h2>
    -  <?php what is the php code to display the category posts by cat id 2 ?>
 * <h2>Category ID 34</h2>
    -  <?php what is the php code to display the category posts by cat id 34 ?>
 * Any help is appreciate before I pull my hair out!@@

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

 *  [dwzemens](https://wordpress.org/support/users/dwzemens/)
 * (@dwzemens)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793305)
 * <?php wp_list_categories(‘orderby=name&include=2’); ?>
 * This code example comes from this help page:
    [Category Help](http://codex.wordpress.org/Template_Tags/wp_list_categories#Include_or_Exclude_Categories)
 * Does this help with your problem?
 *  Thread Starter [chrisb125](https://wordpress.org/support/users/chrisb125/)
 * (@chrisb125)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793312)
 * It displays the categories, but not the post “in” the category. So the category
   id is 34, how do I display the posts from id 34 into the sidebar?
 *  [dwzemens](https://wordpress.org/support/users/dwzemens/)
 * (@dwzemens)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793326)
 * There is probably an easier way, or a plugin, but here is how I accomplished 
   showing a link to each category post, limited by category:
 * <?php
    $myposts = get_posts(‘numberposts=10&category=’); foreach($myposts as 
   $post) : setup_postdata($post); ?> <?php if ( ! in_category(6) ) { ?> <li class
   =’page_item’>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”
   ><?php the_title(); ?> <?php } ?> <?php endforeach; ?>
 * Just change the in_category(6) to include whatever category you wish to have 
   post links displayed from.
 *  Thread Starter [chrisb125](https://wordpress.org/support/users/chrisb125/)
 * (@chrisb125)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793337)
 * Thank you…
 * When adding a link what php tag do I put in here to link to the post?
 * 🙂
 *  [dwzemens](https://wordpress.org/support/users/dwzemens/)
 * (@dwzemens)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793339)
 *     ```
       <ul>
       			<li class="page_item"><h2>Recent News</h2></li>
       		<?php
       			$myposts = get_posts('numberposts=10&category=');
       			foreach($myposts as $post) :
       			setup_postdata($post);
       			?>
       			<?php if ( ! in_category(6) ) { ?>
       				<li class='page_item'><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
       			<?php } ?>
       		<?php endforeach; ?>
       	</ul>
       ```
   
 * For some reason, the earlier message truncated the a href tag. The code above
   should display a link to the post using the Title as the link text.
 *  Thread Starter [chrisb125](https://wordpress.org/support/users/chrisb125/)
 * (@chrisb125)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793377)
 * Ah… thank you so much!
 * Actually I had to add the category # to display the correct category posts:
 * $myposts = get_posts(‘numberposts=10&category=28’);
 * Thanks for the help 🙂
 *  [dwzemens](https://wordpress.org/support/users/dwzemens/)
 * (@dwzemens)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793453)
 * Glad I could help. You are quite welcome!
 *  [jonimueller](https://wordpress.org/support/users/jonimueller/)
 * (@jonimueller)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793460)
 * This may be six of one, half a dozen of the other, but here’s how I did it. You
   can see it (Tech Tips), here: [http://www.znzx.com](http://www.znzx.com)
 * You’ll need to grab your category ID (I inserted the value X), and you can tell
   it how many posts you want it to display (value Y). Change up the heading from
   techtips to whatever your category is.
 *     ```
       <li id="techtips"><h3>Tech Tips</h3>
       <ul>
        <?php
        global $post;
        $myposts = get_posts('numberposts=Y&offset=1&category=X');
        foreach($myposts as $post) :
        ?>
           <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
       </ul>
       ```
   
 *  [y5cafe](https://wordpress.org/support/users/y5cafe/)
 * (@y5cafe)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793714)
 * [@jonimueller](https://wordpress.org/support/users/jonimueller/): Thanks for 
   your post.

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

The topic ‘Code to display posts by category?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 4 participants
 * Last reply from: [y5cafe](https://wordpress.org/support/users/y5cafe/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/code-to-display-posts-by-category/#post-793714)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
