Displaying Posts From A Category In WordPress

It may happen that you are designing some sort of archive pages or a front page and you want to list all posts belonging to some specific category. In this case you can go for following code.

<?php
    $idObj = get_category_by_slug('category_slug_here');
    $id = $idObj-&gt;term_id;
    $posts = get_posts('category='.$id.'&amp;orderby=asc&amp;numberposts=10');
    while(have_posts()) {
        the_post();
        /* Do what ever you want with your posts */
    }
?>

One of the best thing about this code is, it lists all the posts from the specified category and if that specified category has child categories, then the code also lists the posts from child categories. That simply means you will have all posts from specified category and its all child category. You have to replace the ‘/* Do what ever you want with your posts */‘ with appropriate code to handle the post listing on the page.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.