In WordPress how to show specific category post on a page

How to show specific category post on a page? Yes, I am talking about WordPress. In a blog site, there are many posts.By default, all posts of different categories. appear on the blog page.

However, if you want to show post of a specific category on a particular page? For example, you have a few posts under digital marketing category. You want to show all of these posts on a page name digital marketing. And then show the page” digital marketing” on your main menu. This article we will discuss how to show specific category post on a page. Interested? Continue reading.

Create page template in WordPress: Create a file template-category.php file and upload it to your theme folder.

<?php
/**
 * Template Name: Category Custom Page
 */

?>

Go to WordPress dashboard. Create a page where you want to display your specific category post and assign “category custom page” template to this page.

how to show specific category post on a page

Display posts from a specific category: This is the most important step in how to show specific category post on a page. We need to write a code that will fetches posts attached to a specific category. Here is the piece of code that will do the job:

$args = array(
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’,
‘category_name’ => ‘digital marketing’,
‘posts_per_page’ => 5,
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :

while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class=”entry-header”>
<h1 class=”entry-title”><?php the_title(); ?></h1>
</header>
<div class=”entry-content”>
<?php the_excerpt(); ?>
</div>
</article>
<?php
endwhile;
endif;

The above code will only fetches a certain number of posts from that category. But if we need to assign more posts to that category then in such case  we need a paginate links.

In order to accomplish that install and activate the plugin WP-PageNavi. This plugin provides a method WP-PageNavi() which generates the paginate link on the post linking page.

Below is our final code:

<?php
/**
* Template Name: Category Custom Page
*/

get_header(); ?>

<div id=”primary” class=”content-area”>
<main id=”main” class=”site-main” role=”main”>

<?php
$paged = (get_query_var( ‘paged’ )) ? get_query_var( ‘paged’ ) : 1;
$args = array(
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’,
‘category_name’ => ‘digital marketing’,
‘posts_per_page’ => 5,
‘paged’ => $paged,
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :

while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class=”entry-header”>
<h1 class=”entry-title”><?php the_title(); ?></h1>
</header>
<div class=”entry-content”>
<?php the_excerpt(); ?>
</div>
</article>
<?php
endwhile;
wp_pagenavi(
array(
‘query’ => $arr_posts,
)
);
endif;
?>

</main><!– .site-main –>
</div><!– .content-area –>

<?php get_footer(); ?>

Simply copy the above code, paste into your template-category.php file, upload the file to your theme directory. Then create a page ” Digital Marketing”. Assign the page to category custom page template. And see the magic. All of your posts under digital marketing category will appear on the Digital Marketing page. Finally, add the page to your main menu.

That’s it. I hope this article will help you if you need to show your specific category posts on a separate page.

You are free to make any comment. I will apprecitate it greatly.

 

25 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *