<?php
function ed_statements_shortcode($atts) {
    ob_start(); // Start output buffering

    // Define default attributes and extract shortcode attributes
    $atts = shortcode_atts(array(
        'posts_per_page' => 6, // Default value for posts per page
    ), $atts);

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $news_args = array(
        'post_type' => array('post'),
        'posts_per_page' => $atts['posts_per_page'],
        'category_name' => 'statements',
        'paged' => $paged,
    );

    $news_query = new WP_Query($news_args);
    if ($news_query->have_posts()) :
        ?>
        <div class="row">
            <?php while ($news_query->have_posts()) : $news_query->the_post(); ?>
                <div class="col-xl-12 col-lg-12 col-sm-12 ed-news">
                <article>
                <a href="<?= esc_url(get_permalink()); ?>">
                    <div class="card mb-4" data-aos="fade-up">
    			  <div class="row justify-content-between">
    			   <div class="col-xl-3 col-lg-3 col-sm-3 g-0 align-self-center">
    				<a href="<?= esc_url(get_permalink()); ?>">
    						<div class="post-thumb mb-3">
    							<?php // Display the post thumbnail
    								if (has_post_thumbnail()) {
    									the_post_thumbnail('medium'); 
    								}else{
    									echo '<img loading="lazy" src="'. get_template_directory_uri().'/assets/images/photo-placeholder.jpg" alt="NGO Forum Uganda" class="img-fluid"/>';
    								}
    							?>
    						</div>
    						</a>
    				</div>
    				<div class="col-xl-9 col-lg-9 col-sm-9 p-0 align-self-center">
    						<div class="card-body py-0">
    							<div class="post-meta">
    								<span class="date"><?php $post_date = get_the_date(); echo $post_date;?></span>  |  <span class="category">Statements</span>
    							</div>
    							<div class="post-details">
    								<h4><?php the_title(); ?></h4>
    								<span class="excerpt"><?php the_excerpt(); ?></span>
    								<span class="learn-more"><a href="<?= esc_url(get_permalink()); ?>">Learn More</a></span>
    							</div>
    						</div>
    				</div><!-- card body !-->
    				</div>
                        </div>
                </a>
                </article>
                </div>
            <?php endwhile; ?>
        </div>
        
        <?php
        // Pagination
        $big = 999999999; // need an unlikely integer
        echo '<div class="pagination">';
        echo paginate_links(array(
            'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
            'format' => '?paged=%#%',
            'current' => max(1, $paged),
            'total' => $news_query->max_num_pages,
        ));
        echo '</div>';
        wp_reset_postdata();
    else :
        echo 'No news found.';
    endif;

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

// Register the shortcode
add_shortcode('ed_statements', 'ed_statements_shortcode');
?>
