Create a file template-category.php
in your themes directory and add the below comment at the top of file.
template-category.php
Next, go to your WordPress dashboard, create your page where you want to display posts. Assign our template to this page.
Display Posts From Specific Category
We have assigned our template to the WordPress page. Next thing needs to do is write a code which fetches posts attached to a category.
We will use WP_Query
class to fetch the posts. For instance, we assume you have a category called ‘WordPress’ which posts you need to display.
3 | 'post_status' => 'publish' , |
4 | 'category_name' => 'wordpress' , |
7 | $arr_posts = new WP_Query( $args ); |
9 | if ( $arr_posts ->have_posts() ) : |
11 | while ( $arr_posts ->have_posts() ) : |
12 | $arr_posts ->the_post(); |
14 | <article id= "post-<?php the_ID(); ?>" <?php post_class(); ?>> |
16 | if ( has_post_thumbnail() ) : |
20 | <header class = "entry-header" > |
21 | <h1 class = "entry-title" ><?php the_title(); ?></h1> |
23 | <div class = "entry-content" > |
24 | <?php the_excerpt(); ?> |
In the above code we have passed 'category_name' => 'wordpress'
. Here ‘wordpress’ is the slug of a category.
We can also pass category id instead of category_name.
3 | 'post_status' => 'publish' , |
posts_per_page
is the number of posts to fetch from the database. We have used have_posts()
method which checks whether posts are available for WordPress loop. If posts are available then we loop through each post and display it.
Pagination
The code we have written only fetches limited posts from the category. But, what if we have assigned more posts to the category. In that case, we need a paginate links.
For pagination you need to install and activate the plugin
WP-PageNavi.
This plugin provides a method
wp_pagenavi()
which generates a
paginate links on the posts listing page.
To add paginate links in our WordPress page, we need to modify our code. First, we need to pass paged
parameter and then use the function wp_pagenavi()
.
We will get the value for paged as follows:
1 | $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1; |
So below is our final code.
template-category.php
8 | <div id= "primary" class = "content-area" > |
9 | <main id= "main" class = "site-main" role= "main" > |
12 | $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1; |
14 | 'post_type' => 'post' , |
15 | 'post_status' => 'publish' , |
16 | 'category_name' => 'wordpress' , |
17 | 'posts_per_page' => 5, |
20 | $arr_posts = new WP_Query( $args ); |
22 | if ( $arr_posts ->have_posts() ) : |
24 | while ( $arr_posts ->have_posts() ) : |
25 | $arr_posts ->the_post(); |
27 | <article id= "post-<?php the_ID(); ?>" <?php post_class(); ?>> |
29 | if ( has_post_thumbnail() ) : |
33 | <header class = "entry-header" > |
34 | <h1 class = "entry-title" ><?php the_title(); ?></h1> |
36 | <div class = "entry-content" > |
37 | <?php the_excerpt(); ?> |
44 | 'query' => $arr_posts , |
50 | </main><!-- .site-main --> |
51 | </div><!-- .content-area --> |
Of course, you can change the HTML tags to fit with your website design.
We hope you understand how to display posts from the specific category on a WordPress page. Please share your thoughts in the comment section below.
Comments
Post a Comment