Created
December 2, 2024 15:15
-
-
Save wpacademy/f14d186c2d4eca4abe802559fb2c4f06 to your computer and use it in GitHub Desktop.
Directorist plugin categories post count shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add this to your theme's functions.php file or a custom plugin | |
function directorist_count_posts_in_taxonomy_shortcode($atts) { | |
$atts = shortcode_atts( | |
array( | |
'taxonomy' => 'at_biz_dir-category', // Taxonomy name (e.g., 'category', 'post_tag') | |
'term' => '', // Term slug | |
), | |
$atts | |
); | |
if (empty($atts['taxonomy']) || empty($atts['term'])) { | |
return 'Please provide both taxonomy and term.'; | |
} | |
$term = get_term_by('slug', $atts['term'], $atts['taxonomy']); | |
if (!$term || is_wp_error($term)) { | |
return 'Invalid taxonomy or term.'; | |
} | |
return $term->count; | |
} | |
add_shortcode('dierctorist_category_post_count', 'directorist_count_posts_in_taxonomy_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment