0

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

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

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
  • My searches ended up mostly coming up with using query_posts, this works, thanks! – hiiambo Aug 8 '13 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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