By: Prem Tiwari | Last Updated:
In this tutorial, I will explain how to get posts from the category by using the custom query. Sometimes you need to get the posts from a category to display on the webpage with custom designs.
Copy the below code and paste it into your template file OR theme’s PHP files.
$categories = get_the_category();
$category_id = $categories->cat_ID;
$mymain_query = new WP_Query( array( 'cat' => $category_id,'posts_per_page' => '10' ) );
while($mymain_query->have_posts()) : $mymain_query->the_post(); ?>
//shortened code below
<h5><?php the_title(); ?></h5>
<p><?php the_content(): ?></p>
<?php endwhile; ?>
// reset the query
Liked this tutorial? Do Like & share with your friends 🙂
Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies. View all posts by Prem Tiwari