Create Page Template Category Custom Post In WordPress

Create a file template-category.php in your themes directory and add the below comment at the top of file.
template-category.php
1<?php
2/**
3 * Template Name: Category Custom Page
4 */
5 
6?>
Next, go to your WordPress dashboard, create your page where you want to display posts. Assign our template to this page.
Assign Template To 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.
1$args array(
2    'post_type' => 'post',
3    'post_status' => 'publish',
4    'category_name' => 'wordpress',
5    'posts_per_page' => 5,
6);
7$arr_posts new WP_Query( $args );
8 
9if $arr_posts->have_posts() ) :
10 
11    while $arr_posts->have_posts() ) :
12        $arr_posts->the_post();
13        ?>
14        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
15            <?php
16            if ( has_post_thumbnail() ) :
17                the_post_thumbnail();
18            endif;
19            ?>
20            <header class="entry-header">
21                <h1 class="entry-title"><?php the_title(); ?></h1>
22            </header>
23            <div class="entry-content">
24                <?php the_excerpt(); ?>
25            </div>
26        </article>
27        <?php
28    endwhile;
29endif;
In the above code we have passed 'category_name' => 'wordpress'. Here ‘wordpress’ is the slug of a category.
Find Slug Of A Category
We can also pass category id instead of category_name.
1$args array(
2    'post_type' => 'post',
3    'post_status' => 'publish',
4    'cat' => '4'//we can pass comma-separated ids here
5    'posts_per_page' => 5,
6);
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.
Paginate Links
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
1<?php
2/**
3 * Template Name: Category Custom Page
4 */
5 
6get_header(); ?>
7 
8<div id="primary" class="content-area">
9        <main id="main" class="site-main" role="main">
10 
11        <?php
12        $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged') : 1;
13        $args array(
14            'post_type' => 'post',
15            'post_status' => 'publish',
16            'category_name' => 'wordpress',
17            'posts_per_page' => 5,
18            'paged' => $paged,
19        );
20        $arr_posts new WP_Query( $args );
21 
22        if $arr_posts->have_posts() ) :
23 
24            while $arr_posts->have_posts() ) :
25                $arr_posts->the_post();
26                ?>
27                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
28                    <?php
29                    if ( has_post_thumbnail() ) :
30                        the_post_thumbnail();
31                    endif;
32                    ?>
33                    <header class="entry-header">
34                        <h1 class="entry-title"><?php the_title(); ?></h1>
35                    </header>
36                    <div class="entry-content">
37                        <?php the_excerpt(); ?>
38                    </div>
39                </article>
40                <?php
41            endwhile;
42            wp_pagenavi(
43                array(
44                    'query' => $arr_posts,
45                )
46            );
47        endif;
48        ?>
49 
50        </main><!-- .site-main -->
51    </div><!-- .content-area -->
52 
53<?php get_footer(); ?>
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

Popular posts from this blog

How to Add Categories and Tags for WordPress Pages

GALLERY AND ALBUM MANAGEMENT

Customer Chat Plugin (beta)