Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am developing one project and in this project i have to display all the posts related to particular category name.

I have searched a lot but i haven't got any idea to implement this.

How can i do this so that i can display all the posts from particular category/term

share|improve this question
add comment

5 Answers

up vote 6 down vote accepted

Just use WP_Query() to generate your custom query, using the category parameters.

Assuming you know (or know how to get) the ID of the specific category, as $catid:

<?php
$category_query_args = array(
    'cat' => $catid
);

$category_query = new WP_Query( $category_query_args );
?>

Note: you could also pass the category slug to the query, via category_name, instead of cat.

Now, just output your loop:

<?php
if ( $category_query->have_posts() ) : while $category_query->have_posts() : $category_query->the_post();
// Loop output goes here
endwhile; endif;
?>
share|improve this answer
add comment

That would depend on when and how exactly you want to use it - but generally speaking you can either use a custom query , or simply use

if in_category('my_cat_name_or_ID') {
//do whatever
}

if you want to learn about custom query : http://codex.wordpress.org/Custom_Queries

share|improve this answer
    
yes this will work and i have done in other way –  Arpi Patel Feb 1 '12 at 9:00
add comment

WP_Query's tax_query is, far and away, going to be the most flexible way to implement this. If you make the question a bit more specific, I should be able to crank out some sample code for you to get you going.

share|improve this answer
add comment

You can use a plugin (WordPress Category Posts) for that.

WordPress Category Posts is a plugin for WordPress that creates a linked list of the posts in a specific category.

Use the following code wherever you want to list the posts for a category:

wp_cat_posts(get_cat_ID('your_category_name'));

Many thanks.

share|improve this answer
add comment
 query_posts('category_name=my_category_name&showposts=5');
 while (have_posts()) : the_post();
 get_the_content();
 endwhile;
share|improve this answer
    
Apart from the fact that get_the_content() doesn't print anything, please never use query_posts() unless you have a good reason for doing so. –  Stephen Harris Sep 27 '12 at 12:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.