Display Posts From A Specific Category In WordPress
In these tutorials, we study how to display posts from a specific category on WordPress.
In WordPress, by default in post widget, we have all the post of all type we have no method to sperate it by some specific categories.
Display Posts From A Specific Category
To do so we need to firstly, we need to write code.
We need to add code to the file in your themes folder in your computer. the code is in PHP and writes at the top of the file.
<?php /** * Template Name: Category New_WP */ ?>
Then, go to your WordPress dashboard, and create a page where you want to display all the posts.
You should also add the template type that you created above so that all the post are displayed on that page.
Now we have all the posts on a page that we want to display.
Now for getting those posts from a specific category in WordPress, we need to add the code shown below:-
$WP = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'New_WP', 'posts_per_page' => 15, ); $posts_QUERY = new wp_Query( $WP ); if ( $posts_QUERY ->has_posts() ) : while ( $posts_QUERY ->has_posts() ) : $posts_QUERY ->the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php if ( has_post_thumbnail() ) : the_post_thumbnail(); endif; ?> <header class="the-header"> <h1 class="the-title"><?php the_title(); ?></h1> </header> <div class="the-content"> <?php the_excerpt(); ?> <a href="<?php Get_the_permalink(); ?>">Read_More</a> </div> </article> <?php endwhile; endif;
In the above code, we used the WordPress_query for getting a new query for posts of a particular category.
$WP = array( 'post_type' => 'post', 'post_status' => 'publish', 'cat' => '1', //we can pass id here 'posts_per_page' => 15, );
In the above code, we used the cat number instead of its name.
I hope this tutorial will help you to learn how to Display Posts From A Specific Category In WordPress.