Skip to content

Instantly share code, notes, and snippets.

@brunolimadevelopment
Last active May 11, 2020 21:08
Show Gist options
  • Save brunolimadevelopment/822436930497e7c65d598faaef2df644 to your computer and use it in GitHub Desktop.
Save brunolimadevelopment/822436930497e7c65d598faaef2df644 to your computer and use it in GitHub Desktop.
[WP] Exibição de CATEGORIA e SUBCATEGORIA
// exemplo http://casamagalhaes.cvtt.com.br/equipamento/
<ul class="lista-pai">
<li class="lista__head">Categorias</li>
<?php
$taxonomyName = "taxonomy-name";
// Isso obtém apenas os termos da camada superior "CAT PAI". Isso é feito definindo pai como 0.
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );
foreach ( $parent_terms as $pterm ) {
echo '<li class="lista-pai__item">';
echo '<a class="lista-pai__link" href="'.get_term_link( $pterm ).'">'.$pterm->name.'</a>';
echo '<ul class="sub-lista-tipo">';
// pega as "CAT FILHA"
$terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
foreach ( $terms as $term ) {
echo '<li class="sub-lista-tipo__item"><a class="sub-lista-tipo__link" href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
echo '</li>';
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment