How to show posts from a category?

Have you ever wanted to show wordpress posts from a category? Well, a customer had a such requirement recently and I’m going to share my code with some comments here. I know there are number of plugins to do the same, but we have more control when we write our own code in our theme.

In the first step lets write our code snippet in the functions.php file of our theme as a shortcode and use it in theme files. In the next step we will integrate it to a plugin.

Ok, its time to exercise. Open functions.php file of your active theme and copy and past following code in it.

<?php
function unc_show_posts_by_category($attr){
    /** wp query to get posts from specific category */
    $cat_name = $attr['category_name'];
    $num_posts = $attr['num_posts'];
    $unc_query = new WP_Query( array('category_name' => $cat_name, 'posts_per_page' => $num_posts));

    $results = '';
    /** starting the loop */
    if($unc_query->have_posts()):
        $results .= '<ul class="muaw-posts-by-category">';
        while($unc_query->have_posts()){
            $unc_query->the_post();
            $results .= '<li>';
            if(has_post_thumbnail()) {
                $results .= '<a href="' . get_the_permalink() . '" rel="bookmark">' . get_the_post_thumbnail(get_the_ID(), array(50, 50)) . get_the_title() . '</a>';
            } else {
                $results .= '<a href="' . get_the_permalink() . '" rel="bookmark">' .get_the_title() . '</a>';
            }
            $results .= '</li>';
        }
        $results .= '</ul>';
    endif;

    return $results;

    /* Re-set original Post Data */
    wp_reset_postdata();
}

// Add a shortcode
add_shortcode('muaw_category_posts', 'unc_show_posts_by_category');
?>

Now your theme is ready to use shortcode. This function queries to get specific number of posts from a certain category. If featured image is set, this code snippet will display the post thumbnail too. And at the end we defined a shortcode ‘muaw_category_posts’ to simplify it’s usage.

There are 3 different ways to display posts in a certain category using above code.

First method is to call to the function directly in a preferred place inside your theme. To do that simply past the following code in a preferred theme file ( such as page.php, sidebar.php etc… )

<?php
$attr['category_name'] = "uncategorized";
$attr['num_posts'] = 5;
echo unc_show_posts_by_category($attr);
?>

Make sure to change ‘uncategorized’ to a desired category slug and 5 to the number of posts you need to display.

And second method is to call the shortcode inside a theme file. Same as above, past the following code in a desired place of a desired theme file to show posts from a certain category.

<?php echo do_shortcode( '[muaw_category_posts category_name="uncategorized" num_posts="5"]' ); ?>

Here also you will have to change the parameters to fit your needs.

The last method of applying this code to your theme is pretty easy. In the post/page editor, you can past the bellow code and that’s it.

[muaw_category_posts category_name="uncategorized" num_posts="5"]

In order to show the list of posts, you may do some css coding in your theme’s stylesheet. Happy coding!