/* 
 Theme Name:   ASTRA CHILD
 Theme URI:    
 Description:  CHILD FOR ASTRA
 Author:       martha.cottrill
 Author URI:   https://thrivemvmt.com
 Template:     astra
 Version:      1.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html

 /* == Add your own styles below this line ==
--------------------------------------------*/
function posts_by_tag_shortcode($atts) {
    // Extract the tag attribute from the shortcode
    $atts = shortcode_atts(
        array(
            'tag' => '', // Default value for the tag
            'posts_per_page' => 10 // Number of posts to show
        ),
        $atts,
        'posts_by_tag'
    );

    // Set up the query arguments
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => intval($atts['posts_per_page']),
        'tag' => sanitize_text_field($atts['tag'])
    );

    // Create a new query
    $query = new WP_Query($args);
    
    // Start output buffering
    ob_start();

    if ($query->have_posts()) {
        echo '<ul>';
        while ($query->have_posts()) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
    } else {
        echo 'No posts found.';
    }

    // Restore original Post Data
    wp_reset_postdata();

    // Return the output
    return ob_get_clean();
}

// Register the shortcode
add_shortcode('posts_by_tag', 'posts_by_tag_shortcode');


