Skip to content

Instantly share code, notes, and snippets.

@cmcnulty
Created May 5, 2020 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmcnulty/3217c5de3584ecdb4a0c684ef21f2508 to your computer and use it in GitHub Desktop.
Save cmcnulty/3217c5de3584ecdb4a0c684ef21f2508 to your computer and use it in GitHub Desktop.
// function that runs when shortcode is called
function jruuc_themes_shortcode() {
$wcatTerms = get_terms('uu_service_topics', array('hide_empty' => 1, 'parent' =>0));
$message = '<ul class="uu_topics">';
foreach($wcatTerms as $wcatTerm) {
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $wcatTerm->term_id,
'taxonomy' => 'uu_service_topics'
);
$link = get_term_link($wcatTerm->slug, $wcatTerm->taxonomy);
$count = wp_get_topic_postcount($wcatTerm->term_id);
$message .= <<<EOT
<li class="uu_topic uu_topic_{$wcatTerm->slug}">
<a href="{$link}">$wcatTerm->name</a>: $wcatTerm->description - $count service(s)
</li>
EOT;
};
// Output needs to be return
$message .= '</ul>';
return $message;
}
function wp_get_topic_postcount($id) {
$args = array(
'post_type' => 'uu_services',
'post_status' => 'publish', // just tried to find all published post
'posts_per_page' => -1, //show all
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'uu_service_topics',
'field' => 'id',
'terms' => array( $id )
)
)
);
$query = new WP_Query($args);
return (int)$query->post_count;
}
// register shortcode
add_shortcode('service_themes', 'jruuc_themes_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment