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 🙂
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.