function custom_dynamic_count_shortcode($atts) {
// Shortcode attributes allowing 'post_type' and 'taxonomy' to be specified
$atts = shortcode_atts(
array(
'post_type' => 'post', // Default post type
'taxonomy' => 'category', // Default taxonomy
'id' => null, // Default to null, allowing current post ID to be used if not specified
),
$atts,
'dynamic_count'
);
// Use specified ID or fallback to global post ID
$post_id = !empty($atts['id']) ? $atts['id'] : get_the_ID();
// Ensure we have a valid post ID
if ($post_id) {
// Retrieve terms from the specified taxonomy for the given post ID
$terms = wp_get_post_terms($post_id, $atts['taxonomy'], array("fields" => "ids"));
// Count the terms and subtract 1
$term_count = count($terms) - 1;
// Check if adjusted terms count is 0 or less
if ($term_count > 0) {
return "+" . $term_count;
}
}
return ""; // Return empty string if conditions aren't met or if adjusted count is 0 or less
}
// Register the shortcode with WordPress
add_shortcode('dynamic_count', 'custom_dynamic_count_shortcode');