Skip to content

Instantly share code, notes, and snippets.

@DanBeckett
Created November 18, 2014 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanBeckett/8d878be7255e6bcd079a to your computer and use it in GitHub Desktop.
Save DanBeckett/8d878be7255e6bcd079a to your computer and use it in GitHub Desktop.
List WordPress category terms, apply parent class to any terms with children
<?php
$terms = get_terms('category');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach( get_terms( 'category', array( 'hide_empty' => false, 'parent' => 0 ) ) as $parent_term ) {
$term_children = get_term_children($parent_term->term_id, 'category'); ?>
<li class="term_item<?php echo ($term_children ? ' parent' : ''); ?>" data-hook="<?php echo $parent_term->slug; ?>">
<a href="<?php echo home_url(); ?>/products/<?php echo $parent_term->slug; ?>"><?php echo $parent_term->name; ?></a>
</li>
<?php }
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment