query order by category

i have this line.. in sidebar..

<?php query_posts ( ( array('orderby' => 'rand', 'showposts' => 12)));?>

i want to order by category not random..
so if i am in a post from category animals.. i want to show 12 post just from this category.
Not Random like now..

Read More

how can i do that?

Related posts

Leave a Reply

1 comment

  1. This should limit the posts pulled in the new WP_Query instance to the currently selected category or the first category of the current post.

    global $wp_query;
    $sidebar_query_vars = array('orderby' => 'rand', 'showposts' => 12);
    if(is_category()) {
        $sidebar_query_vars['cat'] = $wp_query->get_queried_object()->term_id;
    } elseif (is_single()) {
        $categories = (get_the_terms($wp_query->get_queried_object_id(), 'category'));
        if(is_array($categories)) {
            $first_cat = array_shift($categories);
            $sidebar_query_vars['cat'] = $first_cat->term_id;
        }
    }
    $sidebar_query = new WP_Query($sidebar_query_vars);
    while($sidebar_query->have_posts()): $sidebar_query->the_post();
    //do output here
    endwhile;