Skip to content

Instantly share code, notes, and snippets.

@wpacademy
Created December 2, 2024 15:15
Show Gist options
  • Save wpacademy/f14d186c2d4eca4abe802559fb2c4f06 to your computer and use it in GitHub Desktop.
Save wpacademy/f14d186c2d4eca4abe802559fb2c4f06 to your computer and use it in GitHub Desktop.
Directorist plugin categories post count shortcode
<?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