<?php

//Home Latest News 
function homeNews_shortcode() {
    ob_start(); // Start output buffering
    
	$news_args = array(
		'post_type' => array('post'),
		'posts_per_page' => 3,
		'category_name' => 'news, blog',
	);
 ?>

    <?php $news_query = new WP_Query($news_args);?>
		<?php if ($news_query->have_posts()) : ?>
		<div class="row">
			<?php while ($news_query->have_posts()) :
				$news_query->the_post();
				$categories = get_the_category();
			?>
			<div class="col-md-4 col-sm-6 kpy-news">
				<article>
					<a href="<?= esc_url(get_permalink()); ?>">
						<div class="card  mb-3 border-0" data-aos="fade-up">
							<div class="post-thumbnail">
								<?php // Display the post thumbnail
									if (has_post_thumbnail()) {
										the_post_thumbnail('medium'); 
									}else{
										echo '<img src="'. get_template_directory_uri().'/assets/images/photo-placeholder.jpg" alt="NGO" class="img-fluid"/>';
									}
								?>
								
							</div>
							<div class="card-body shadow-sm">
								<h3><?php the_title(); ?></h3>
								<?php the_excerpt(); ?>
								<a href="<?= esc_url(get_permalink()); ?>" class="btn btn-outline-info read-more">Read More</a>
							</div><!-- card body !-->
						</div><!-- card !-->
						</a>
				</article>
			</div><!-- col !-->
			<?php endwhile;
				wp_reset_postdata();
			else :
				echo 'No news found.';
			endif;
			?>
	</div><!-- Row !-->
    <?php

    return ob_get_clean(); 
}

// Register the shortcode
add_shortcode('front-news', 'homeNews_shortcode');

?>