Reputation: 17315
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 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
Upvotes: 4
Views: 29771
Reputation: 350
I have filtered post by category Id using the method below:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* 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
*/
$is_post_wrap++;
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;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
Upvotes: 0
Reputation: 11
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.
This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.
Upvotes: 1
Reputation: 1
thank you for sharing on your thought its a great thought. 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
Upvotes: -1
Reputation: 32629
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/
Upvotes: 0
Reputation: 415
http://codex.wordpress.org/Template_Tags/query_posts
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.
Upvotes: 0
Reputation: 507
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.
Upvotes: 0
Reputation: 9262
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).
Upvotes: 0
Reputation: 154
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.
Upvotes: 11
Reputation: 49344
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.
Upvotes: 1