Created
May 5, 2020 03:23
-
-
Save cmcnulty/3217c5de3584ecdb4a0c684ef21f2508 to your computer and use it in GitHub Desktop.
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
// 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