List Recent Posts from Specific Category – WordPress

You can list post from specific category by query_posts() function. it will take one or two parameters, one is category name and 2nd is no of post to show. like following code is showing exactly how it work.

<?php query_posts('category_name=internet&showposts=5'); ?>

and full code to show posts

<?php query_posts('category_name=internet&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>  </li>
        <?php endwhile; ?>

and for normal code is here:

<?php while (have_posts()) : the_post(); ?>
 <li><a href="<?php the_permalink(); ?>">
 <?php the_title(); ?>
 </a> </li>
 <?php endwhile; ?>

query_posts() is a function which can also be used for custom post type. if you have some custom post type and want to show posts from , you can use this function. its take many parameters on the basis of which you can collect your required posts from db. in that case you will use to pass a list of array to this function and it will grab you required data.

query_posts( $args );

or you can pass directly to function.

query_posts( 'cat=3&year=2004&tag='phone'' );

in above code it will collect posts from category=3, year=2004 and tag=’phone’. so overall its amazing function to grab custom data from WordPress and can reduce your hurdle many times.