Created
June 7, 2018 13:26
-
-
Save Tusko/71824b672713573f475399b8a8006d27 to your computer and use it in GitHub Desktop.
Wordpress Taxonomy Tree
This file contains 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
.is-current-cat { | |
color: red !important | |
} | |
.cat-parent > li > ul { | |
padding-left: 20px; | |
display: none | |
} | |
.cat-parent > li > .is-current-cat + ul { | |
display: block | |
} |
This file contains 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
<?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