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

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

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $news_args = array(
        'post_type' => array('page'),
        'posts_per_page' => $atts['posts_per_page'],
        'category_name' => 'civil-society',
        '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-md-3 col-sm-6 h-100">
            <article>
                <a href="<?= esc_url(get_permalink()); ?>">
                    <div class="card shadow-sm border-0" data-aos="fade-up">
                        <div class="post-thumbnail">
                            <?php // Display the post thumbnail
                            if (has_post_thumbnail()) {
                                the_post_thumbnail('full', ['class' => 'img-fluid']);
                            } else {
                                echo '<img src="' . get_template_directory_uri() . '/assets/images/placeholder-image.jpg" alt="NGO FORUM" class="img-fluid"/>';
                            }
                            ?>
                        </div>
                        <div class="card-body">
                            <h5><?php the_title(); ?></h5>
                            <a href="<?= esc_url(get_permalink()); ?>" class="btn btn-outline-info read-more">Project Details </a>
                        </div><!-- card body -->
                    </div><!-- card -->
                </a>
            </article>
        </div><!-- col -->
        <?php endwhile;
        wp_reset_postdata();
        ?>
    </div><!-- row -->
    <?php
    else :
        echo 'No related projects.';
    endif;

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

// Register the shortcode
add_shortcode('civic-projects', 'CProjects_shortcode');
?>
