costumed query in wordpress category.php

I create category.php for display categories archive in a costumed template.

In a category page link like this: http://www.example.com/category/cat1/

Read More

By these codes it’s OK and shows last items of cat1

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Some template code
<?php endwhile; ?>
<?php endif; ?> 

But when I try to customize query by WP_Query or query_posts instead of contents of cat1 it shows contents of all categories of site

    <?php query_posts( 'posts_per_page=30' ); ?>    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    // Some template code
    <?php endwhile; ?>
    <?php endif; ?> 

What is reason and solution?

Related posts

Leave a Reply

1 comment

  1. You must define cat in your query.

    it’s your answer:

    <?php
    $args = array(
        'cat' => get_query_var('cat'),
            'posts_per_page' => 30
    );
    $recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
     //some template code
    
    <?php endwhile; ?>