banner



How To Search For Blogs On Wordpress

Do you want to display posts from a specific category on a custom page template in WordPress? Using a page template, you can use your own design to display the post listing on a WordPress page. In this article, we study how to display posts from a specific category on a custom page template.

In WordPress, you can categorize your post. This feature is useful to find out all posts that come under a specific category. When a user clicks on one of the categories they can see a post listing page for that specific category. WordPress uses the following template files for rendering posts of a category.

  • category-slug.php
  • category-ID.php
  • category.php
  • archive.php
  • index.php

To display posts of the specific category, WordPress searches for template files in the above order. Whichever template file found first, code from that file will be used to display posts listing of a category.

It's all about the default WordPress templates. But, what if someone wants to use a custom page template to list the posts of a specific category?

Let's go through the steps below to achieve this goal.

Create Page Template in WordPress

Our goal is to display posts by category on a page. Obviously, you need to write a code for it. Create a file template-category.php in your active theme's directory and add the below comment at the top of a file.

template-category.php

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

Next, go to your WordPress dashboard, create the page where you want to display posts. Assign the above template to this newly created page.

Assign Template To Page

There is another method to assign a template file to your WordPress page. In that case, you have to create a file in your theme in the format page-{slug}.php. Let's say slug of your page is category-list then your file name would be page-category-list.php. After this, the code from this file is being used for your page.

Display Posts from Specific Category on a WordPress Page

You have created and assigned your template to the WordPress page. Now, let's write a code that fetches posts attached to a category.

I will use WP_Query class to fetch the posts. For instance, I assume you have a category called 'WordPress' and you want to display posts attached to this category.

Write the code below to get a list of posts under the 'WordPress' category.

$args = array(     'post_type' => 'post',     'post_status' => 'publish',     'category_name' => 'wordpress',     '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(); ?>                 <a href="<?php the_permalink(); ?>">Read More</a>             </div>         </article>         <?php     endwhile; endif;          

In the above code, I used the 'category_name' => 'wordpress'. Here 'wordpress' is the slug of a category.

Find Slug Of A Category

The user can also pass the category id instead of category_name. In that case, you should use the key as 'cat' in place of 'category_name' and pass the id of a category.

$args = array(     'post_type' => 'post',     'post_status' => 'publish',     'cat' => '4', //you can pass comma-separated ids here     'posts_per_page' => 5, );          

The value for posts_per_page is the number of posts to fetch from the database. I have used the have_posts() method which loops through each post and displays it.

Get Posts from Custom Taxonomy

You may be working with a custom post type and wish to display the posts from a custom taxonomy. For that, you need to change arguments in the previous array as follows:

$args = array(     'post_type' => 'CUSTOM_POST_TYPE_NAME',     'post_status' => 'publish',     'posts_per_page' => 5,     'tax_query' => array(         array(             'taxonomy' => 'TAXONOMY_NAME',             'field'    => 'slug',             'terms'    => array( 'TERM_SLUG' ),             'operator' => 'IN'         ),     ), );   $arr_posts = new WP_Query( $args );          

No need to change the other part of code. The above arguments will get the posts of a specified taxonomy.

Pagination

The code I have written only fetches limited posts from the category. Although you can get any number of posts by passing a number to 'posts_per_page', we usually display limited posts per page and then use pagination to get the next and previous set of posts.

In order to integrate the pagination, you need to install and activate the WP-PageNavi plugin. This plugin provides a method wp_pagenavi() that generates pagination links like the below screenshot. These links will be used to see the next or previous posts.

Paginate Links

To add these pagination links to your WordPress page, you need to modify your code. First, you need to pass paged parameter and then use the function wp_pagenavi().

You will get the value for paged key as follows:

$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;          

Our final code is as follows.

template-category.php

<?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' => 'wordpress',         '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(); ?>                     <a href="<?php the_permalink(); ?>">Read More</a>                 </div>             </article>             <?php         endwhile;         wp_pagenavi(             array(                 'query' => $arr_posts,             )         );     endif;     ?>       </main><!-- .site-main --> </div><!-- .content-area -->   <?php get_footer(); ?>          

We have created a basic page for our listing. You can create a nicely crafted page for your posts listing by applying styling to it.

I hope you understand how to display posts from a specific category on a WordPress page. Please share your thoughts and suggestions in the comment section below.

Related Articles

  • How to Load WordPress Post with AJAX
  • How to Set Featured Image Programmatically in WordPress
  • How to Set Correct File Permissions for WordPress

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

How To Search For Blogs On Wordpress

Source: https://artisansweb.net/display-posts-specific-category-wordpress-page/

Posted by: crawfordhaterreact1959.blogspot.com

Related Posts

0 Response to "How To Search For Blogs On Wordpress"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel