function display_latest_case_studies_posts($atts) {
// Extract the attributes passed to the shortcode
$atts = shortcode_atts(
array(
'category' => 'case-studies', // Default category slug for case studies
'posts_per_page' => 5, // Number of posts to display
),
$atts,
'latest_case_studies_posts'
);
// Query arguments
$args = array(
'category_name' => $atts['category'],
'posts_per_page' => $atts['posts_per_page'],
'post_status' => 'publish',
);
// Query the posts
$query = new WP_Query($args);
// Start the output buffer
ob_start();
// Check if there are posts
if ($query->have_posts()) {
echo '<div class="latest-case-studies-posts">';
while ($query->have_posts()) {
$query->the_post();
echo '<div class="post">';
echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
echo '<div class="excerpt">' . get_the_excerpt() . '</div>';
echo '</div>';
}
echo '</div>';
} else {
echo '<p>No posts found.</p>';
}
// Reset post data
wp_reset_postdata();
// Get the output buffer content
return ob_get_clean();
}
// Register the shortcode
add_shortcode('latest_case_studies_posts', 'display_latest_case_studies_posts');
You must be logged in to post a comment.