Skip to content

Instantly share code, notes, and snippets.

@sirBlond
Created September 5, 2016 18:23
Show Gist options
  • Save sirBlond/1cc3263d9849f37492dda28c065f4ff9 to your computer and use it in GitHub Desktop.
Save sirBlond/1cc3263d9849f37492dda28c065f4ff9 to your computer and use it in GitHub Desktop.
get terms list
function naked_print_tax_summary($tax = 'category') {
$tax_summary = naked_get_tax_summary($tax);
if(!empty($tax_summary)) {
ob_start(); ?>
<ul>
<?php foreach($tax_summary as $tax_item): ?>
<li>
<a href="<?= $tax_item['link']; ?>"><?= $tax_item['name']; ?></a>
<span class="posts-count"><?= $tax_item['count']; ?></span>
<?php if(isset($tax_item['childs']) && !empty($tax_item['childs'])): ?>
<ul class="has-childs">
<?php foreach($tax_item['childs'] as $child_item): ?>
<li>
<a href="<?= $child_item['link']; ?>"><?= $child_item['name']; ?></a>
<span class="posts-count"><?= $child_item['count']; ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
$html = ob_get_clean();
}
print($html);
}
function naked_get_tax_summary($tax) {
$summary = array();
$tax_terms = get_terms($tax, array('hide_empty' => false));
if(is_wp_error($tax_terms))
return array();
foreach ($tax_terms as $tax_term_obj) {
if($tax_term_obj->parent) {
$summary[$tax_term_obj->parent]['childs'][$tax_term_obj->term_id] = array(
'id' => $tax_term_obj->term_id,
'name' => $tax_term_obj->name,
'link' => get_term_link($tax_term_obj),
'count' => $tax_term_obj->count
);
}else{
$summary[$tax_term_obj->term_id] = array(
'id' => $tax_term_obj->term_id,
'name' => $tax_term_obj->name,
'link' => get_term_link($tax_term_obj),
'count' => $tax_term_obj->count,
);
}
}
return $summary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment