Skip to content

Instantly share code, notes, and snippets.

@diggeddy
Last active February 3, 2023 09:27
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 diggeddy/38ebe26b6ed9fbd6c1ad0636ba70ff73 to your computer and use it in GitHub Desktop.
Save diggeddy/38ebe26b6ed9fbd6c1ad0636ba70ff73 to your computer and use it in GitHub Desktop.
display terms that match query heirarchy
<?php
// terms that match the tax archives heierarchy
// show only parent terms in parent archives
// show only child terms in child archives
function custom_get_terms() {
// set the taxonomy
$tax_name = 'category';
// is archive parent or child logic
$current_term = get_queried_object();
$is_parent = $current_term->parent == 0 ? true : false;
// get the terms
$terms = get_the_terms( get_the_ID(), $tax_name );
$html = '<ul class="custom-terms">';
foreach( $terms as $key => $term ) {
if ( ( $is_parent && $term->parent == 0 ) || ( !$is_parent && $term->parent != 0 ) ) {
// return html if term heirarchy matches archives
$html .= '<li><a href="' . esc_attr( get_term_link( $term->slug, $tax_name ) ) . '">' . __( $term->name ) . '</a></li>';
}
}
$html .= '</ul>';
return $html;
}
// example use in render_block filter
add_filter( 'render_block', function( $block_content, $block ) {
if (
! empty( $block['attrs']['className'] )
&& 'custom-terms' === $block['attrs']['className']
) {
$block_content = custom_get_terms();
}
return $block_content;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment