Skip to content

Instantly share code, notes, and snippets.

@dantetesta
Created November 29, 2023 01:29
Show Gist options
  • Save dantetesta/b4c800eda11e31bc8203d1f70eaccf4c to your computer and use it in GitHub Desktop.
Save dantetesta/b4c800eda11e31bc8203d1f70eaccf4c to your computer and use it in GitHub Desktop.
function show_taxonomy_lista() {
$nome_tax = 'estados';
$CSS = '
<style>
.tax-style {
font-size: 16px;
color: #333333;
}
.tax-parent {
font-weight: bold;
}
.tax-child {
margin-left: 10px;
}
</style>
';
$rd_terms = wp_get_post_terms(get_the_ID(), $nome_tax, array("fields" => "ids"));
if ($rd_terms) {
$term_array = trim(implode(',', (array) $rd_terms), ' ,');
$neworderterms = get_terms($nome_tax, 'orderby=none&include=' . $term_array);
$output = '';
foreach ($neworderterms as $orderterm) {
if ($orderterm->parent == 0) {
$output .= '<div class="tax-style tax-parent">' . $orderterm->name . '</div>';
$children = get_terms($nome_tax, array('parent' => $orderterm->term_id, 'orderby' => 'none'));
if (!empty($children)) {
foreach ($children as $child) {
if(in_array($child->term_id, $rd_terms)){
$output .= '<div class="tax-style tax-child">' . $child->name . '</div>';
}
}
}
$output .= '<br>';
}
}
echo $CSS . '<div class="tax-style">' . $output . '</div>';
}
}
add_shortcode('tax_list', 'show_taxonomy_lista');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment