get category post in wordpress
Step 1: Open the WordPress theme files where you want to display the category posts.
Step 2: Locate the section of the code where you want to display the category posts.
Step 3: Use the get_categories
function to retrieve the categories.
Step 4: Use a foreach loop to iterate through the categories.
Step 5: Inside the loop, use the get_posts
function to retrieve posts for each category.
Step 6: Display the post information as needed using the loop.
Here is an example code snippet:
<?php
// Step 1: Open the WordPress theme files where you want to display the category posts.
// Step 2: Locate the section of the code where you want to display the category posts.
// Step 3: Use the get_categories function to retrieve the categories.
$categories = get_categories();
// Step 4: Use a foreach loop to iterate through the categories.
foreach ($categories as $category) {
// Step 5: Inside the loop, use the get_posts function to retrieve posts for each category.
$args = array(
'category' => $category->cat_ID,
'posts_per_page' => 5, // Adjust the number of posts as needed
);
$category_posts = get_posts($args);
// Step 6: Display the post information as needed using the loop.
foreach ($category_posts as $post) {
setup_postdata($post);
?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
<?php
}
wp_reset_postdata();
}
?>
Note: Customize the code according to your specific requirements and where you want to display the category posts in your theme.