How to show most recent post from each Category WordPress

Hello friends, welcome to my another blog post how to show most recent post from each Category WordPress. If you want to create a custom page where you want to show most recent post from individual category then you must use below code. This code will help to achieve your solution.

At first I fetch all categories using get_categories() function. Then loop through each category and show recent 10 posts from each category.



<?php
    $categories = get_categories(); //returns an array of category objects 
	
    foreach ( $categories as $category ) {
		/*
			$category->term_id
			$category->name
			$category->slug
			$category->term_group
			$category->term_taxonomy_id
			$category->taxonomy
			$category->description
			$category->parent
			$category->count
			$category->cat_ID
			$category->category_count
			$category->category_description
			$category->cat_name
			$category->category_nicename
			$category->category_parent
		*/	
		$args = array(
		'numberposts' => 10,
		'offset' => 0,
		'cat' => $category->term_id,
		'orderby' => 'post_date',
		'order' => 'DESC',
		'include' => ,
		'exclude' => ,
		'meta_key' => ,
		'meta_value' =>,
		'post_type' => 'post',
		'post_status' => 'publish',
		'suppress_filters' => true );

		$query = wp_get_recent_posts( $args, ARRAY_A );
 
		if ( $query->have_posts() ) { ?>
		 
			<section class="<?php echo $category->name; ?> listing">
				<h2>Latest in <?php echo $category->name; ?>:</h2>
		 
				<?php while ( $query->have_posts() ) {
		 
					$query->the_post();
					?>
		 
					<div id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
						<?php if ( has_post_thumbnail() ) { ?>
							<a href="<?php the_permalink(); ?>">
								<?php the_post_thumbnail( 'thumbnail' ); ?>
							</a>
						<?php } ?>
		 
						<h3 class="entry-title">
							<a href="<?php the_permalink(); ?>">
								<?php the_title(); ?>
							</a>
						</h3>
		 
						<?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>
		 
					</div>
		 
				<?php } // end while ?>
		 
			</section>
		 
		<?php } // end if
 
	wp_reset_postdata(); // Use reset to restore original query.
    }
?>

Please share my blog post. If you have any opinion please comment below. Thank you.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •