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 recently got some help here on getting a few posts to display in block format. I was wondering if someone could assist me with modifying the code to limit the posts that show up either by category or by tag. I've listed the code below:

<div id="mini_stream">
    <ul>
<? $args = array(
    'post_type' => 'post',
    'posts_per_page' => 4
);

$loop = new wp_Query($args);

while($loop->have_posts()) : $loop->the_post();
    echo '<a href="'.get_permalink().'">';
    echo get_the_post_thumbnail($post->ID, 'category-thumb');
    the_title( '<h6>', '</h6>' );
    echo '</a>';
endwhile;

wp_reset_query(); ?>
    </ul>
</div>
share|improve this question
    
It sounds like you need something like this: wordpress.stackexchange.com/a/100016/21376 If that doesn't work, explain why. How is your case different? –  s_ha_dum Aug 8 '13 at 4:21

2 Answers 2

up vote 1 down vote accepted

Add category_name or cat in your arguments (args) array.

<div id="mini_stream">
    <ul>
<? $args = array(
    'post_type' => 'post',
    'posts_per_page' => 4,
    'category_name'=>'html',
);

$loop = new wp_Query($args);

while($loop->have_posts()) : $loop->the_post();
    echo '<a href="'.get_permalink().'">';
    echo get_the_post_thumbnail($post->ID, 'category-thumb');
    the_title( '<h6>', '</h6>' );
    echo '</a>';
endwhile;

wp_reset_query(); ?>
    </ul>
</div>
share|improve this answer
    
There is no need to call wp_reset_query() in this case. This code will also generate incorrect HTML output (you don't output <li> anywhere in it, so this list won't be correct HTML). –  Krzysiek Dróżdż Aug 8 '13 at 13:48

For tags you can use:

<?php
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 4,
        'tag' => 'cooking'
    );
    ... REST OF YOUR CODE

Other parameters you can use for tags are:

  • tag (string) - use tag slug.
  • tag_id (int) - use tag id.
  • tag__and (array) - use tag ids.
  • tag__in (array) - use tag ids.
  • tag__not_in (array) - use tag ids.
  • tag_slug__and (array) - use tag slugs.
  • tag_slug__in (array) - use tag slugs.

For categories you can use:

<?php
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 4,
        'category_name' => 'cooking'
    );
    ... REST OF YOUR CODE

And other parameters you can use for categories are:

  • cat (int) - use category id.
  • category_name (string) - use category slug (NOT name).
  • category__and (array) - use category id.
  • category__in (array) - use category id.
  • category__not_in (array) - use category id.

Links to Codex (have you even tried to search?):

  1. http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
  2. http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
share|improve this answer
    
My searches ended up mostly coming up with using query_posts, this works, thanks! –  Bo Shubinsky Aug 8 '13 at 11:16

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.