WordPress: How to display only posts that are in a certain category?


I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..

However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.

Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters

I have a post that if I go to it directly works correctly, but does not show up on this page.

The post direct link.

The category id is '3' while the category name is 'typographic-posters'.

I have a custom page template for the typographic-posters page that looks like this:

Template Name: Typographic Posters

<?php get_header(); ?>
<?php get_sidebar(); ?>

<?php if (in_category('3')): ?>
<div class="post">

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

  <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
   <div class="post-description">
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>

    <?php endwhile; else: ?>
     <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

<?php endif; ?>

<?php get_footer(); ?>

Using this code however the page only shows gets the header, sidebar and nothing else..

If someone could help me out that would really help me get a handle on this filtering of wordpress categories.

Thanks for reading,


10/4/2009 6:33:17 AM

Accepted Answer

in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.

Once obtained, just use the normal WordPress loop to access these posts.

10/4/2009 6:56:03 AM

I have filtered post by category Id using the method below:

                if (have_posts()) : while (have_posts()) :

                // if(1) {
                    //echo the_category_ID();
                 * The default post formatting from the post.php template file will be used.
                 * If you want to customize the post formatting for your homepage:
                 *   - Create a new file: post-homepage.php
                 *   - Copy/Paste the content of post.php to post-homepage.php
                 *   - Edit and customize the post-homepage.php file for your needs.
                 * Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part

                    if($is_post_wrap == '1') {
                        ?><div class="post-wrap clearfix"><?php
                    get_template_part('post', 'homepage');

                    if($is_post_wrap == '3') {
                        $is_post_wrap = 0;


            else :
                get_template_part('post', 'noresults');

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow