<?php
function allSponsors_shortcode($atts) {


	$atts = shortcode_atts(
        array(
            'category' => 'sponsor', // Default to empty string if not specified
            'number' => 14, // Default number of posts per page
            'order' => 'RAND', //Default Descending order
        ),
        $atts,
        'sponsor'
    );

    // Custom query to retrieve sponsors
    $sponsors_args = array(
        'post_type' => 'sponsor',
        'posts_per_page' => $atts['number'], // Number of posts per page
        'orderby' => 'date', // Order by date
        'order' => $atts['order'], // order
    );

    // Check if category attribute is provided and not empty
    if (!empty($atts['category'])) {
        $sponsors_args['tax_query'] = array(
            array(
                'taxonomy' => 'sponsor_category',
                'field' => 'slug',
                'terms' => $atts['category'],
            ),
        );
    }

    $sponsors_query = new WP_Query($sponsors_args);
	?>																
	<?php $sponsors_query = new WP_Query($sponsors_args);?>
		<?php if ($sponsors_query->have_posts()) : ?>
		<div class="row">
			<?php while ($sponsors_query->have_posts()) :
				$sponsors_query->the_post();
				$categories = get_the_category();
            $website        = get_post_meta(get_the_ID(), '_website', true);
				
			?>
			<div class="col-xl-3 col-lg-4 col-sm-6 kpy-news">
				<article>
					<a href="<?= esc_url($website); ?>" target="_blank">
						<div class="card shadow-sm 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">
								<h3><?php the_title(); ?></h3>
								<div class="catz">
									<i class="bi bi-calendar-check"></i> <?php
									$post_date = get_the_date();
									echo $post_date;
									?>
								</div><!-- Date !-->
								<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 sponsors/associates found.';
			endif;
			?>
	</div><!-- Row !-->
    <?php

    return ob_get_clean(); // Return the buffered content
}

// Register the shortcode
add_shortcode('hads-sponsors', 'allSponsors_shortcode');

?>