I jus want to create a gallery which shows either all or certain number of posts in that category under each post and i found a working code

$displayposts = new WP_Query();
  //get posts from your news category
while ($displayposts->have_posts()) : $displayposts->the_post();
<?php the_content('read more...'); ?>
<?php edit_post_link('edit', '', ''); ?>
<?php endwhile; ?>

in the above code i want to enter the specific category instead of cat=5

1 Answer 1


Well first off, that code's a mess to read, some formatting would definitely help get you an answer. That said, here's the documentation on WP_Query and some sample code to boot

$args = array(
    'post_status' => 'publish',
    'tax_query'   => array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => 'your_cat_slug'
$posts = new WP_Query( $args );

I've used 'tax_query' rather than 'category_name' because I feel it is more flexible while being more understandable (and thus easy to use) at the same time, if you have any issues with understanding the code, read the documentation, WP_Query is SO well documented it's crazy.


Updated code

$cats = array();
foreach( get_the_category() as $cat ) {
    $cats[] = $cat->cat_ID;
$args = array(
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
            'taxonomy'         => 'category',
            'field'            => 'id',
            'terms'            => $cats,
            'include_children' => false
print_r( WP_Query( $args ) as $post );
  • I am sorry to tell this, I am a noob in WP and i dont either know PHP.please tell me how get the posts from specific category or what i should put instead of cat=4 for that current category
    – Felix
    Commented Jan 17, 2012 at 11:01
  • What part of the code isn't clear? You've asked the same question you asked in the OP, which I have already answered (or tried to answer) to the best of my ability. If you can tell me what, specifically, is wrong, I can probably update the code for you.
    – mor7ifer
    Commented Jan 17, 2012 at 11:44
  • i guess my language is making it little complex. so please apologies me.First thing is i dont know how to use yur above code. I tried the code which i posted and it worked for me. I got all the posts but the problem is I have to give the value of the category say cat=5.what i want is to code it in such a way it gets the category code and display all the posts rather than i assigning the value
    – Felix
    Commented Jan 17, 2012 at 12:18
  • Would you rather give it the category ID or the category slug? Those are the only two options, since category names can be duplicated.
    – mor7ifer
    Commented Jan 17, 2012 at 12:26
  • Is there any other code to do it without givin either category ID or category style?? I hate spoon feeding but i dunno nothing about codes so only asking too much of questions.Sorry for annoyin yu :(
    – Felix
    Commented Jan 17, 2012 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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