Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Tusko
Created June 7, 2018 13:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tusko/71824b672713573f475399b8a8006d27 to your computer and use it in GitHub Desktop.
Save Tusko/71824b672713573f475399b8a8006d27 to your computer and use it in GitHub Desktop.
Wordpress Taxonomy Tree
.is-current-cat {
color: red !important
}
.cat-parent > li > ul {
padding-left: 20px;
display: none
}
.cat-parent > li > .is-current-cat + ul {
display: block
}
<?php
function wpa_get_tax_tree($parent) {
global $wp_query;
$tax = $wp_query->queried_object->taxonomy;
$terms = get_terms($tax, array( "orderby" => "slug", "parent" => $parent, 'hide_empty' => 0 ) );
$currTerm = $wp_query->queried_object;
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
echo '<ul class="cat-parent">';
foreach ( $terms as $term ) {
$hasChild = get_term_children( $term->term_id, $tax );
?>
<li>
<a href="<?php echo get_term_link($term); ?>" class="<?php echo ($currTerm->term_id === $term->term_id || in_array($currTerm->term_id, $hasChild) )?'is-current-cat':''; ?>"><?php echo $term->name; ?></a>
<?php if($hasChild) laser_get_tax_tree($term->term_id); ?>
</li>
<?php }
echo '</ul>';
}
}
wpa_get_tax_tree(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment