views:

933

answers:

6

Hi,

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:

<?php
/*
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(); ?>
   </div>
   <?=get_image('flutter-image');?>
  </div>


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

</div>
<?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,

Jannis

+1  A: 

The easiest way is to create a file called category-3.php and use the standard code from normal index.php or category.php file. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.

Eimantas
+4  A: 

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.

Kvis
Thanks, this works nicely. Can't believe I didn't manage to do find this myself :) Thanks again.
Jannis
A: 

Simply add before the loop:

<?php query_posts="cat=3&showposts=5">

This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).

konzepz
A: 

I would 2nd Eimantas' suggestion. The Template Hierarchy will use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.

But if you need to stick with a Page to display those posts, also see the Page of Posts example.

Michael
that does look like a good way to go, though I am not entirely sure if I want the page slug to become domain.com/category-3.php or is this something customizable?
Jannis
With default permalinks, url would be http://youdomain/?cat=8
Michael
A: 

http://codex.wordpress.org/Template%5FTags/query%5Fposts

Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.

Adam
thanks for the link. will dig into the codex a bit more for sure.
Jannis
A: 

This plugin could also help you if you want to be able to change the displayed categories without going through the code : http://wordpress.org/extend/plugins/advanced-category-excluder/

Damien MATHIEU
looks like a great plugin, will definitely give it a try.thanks!
Jannis