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 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

<?php
$displayposts = new WP_Query();
  //get posts from your news category
  $displayposts->query('cat=5');
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

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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(
        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.

edit

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(
        array(
            'taxonomy'         => 'category',
            'field'            => 'id',
            'terms'            => $cats,
            'include_children' => false
        )
    )
);
print_r( WP_Query( $args ) as $post );
share|improve this answer
    
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 Jan 17 '12 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. –  m0r7if3r Jan 17 '12 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 Jan 17 '12 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. –  m0r7if3r Jan 17 '12 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 Jan 17 '12 at 12:39

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.