Skip to content

Instantly share code, notes, and snippets.

@01100001
Forked from remcotolsma/functions.php
Created July 24, 2020 14:53
Show Gist options
  • Save 01100001/bf6da3fefde15d34979e35c0d2529e3d to your computer and use it in GitHub Desktop.
Save 01100001/bf6da3fefde15d34979e35c0d2529e3d to your computer and use it in GitHub Desktop.
Custom WordPress the_terms() function to sort the post terms by "Category Order and Taxonomy Terms Order".
<?php
/**
* The terms
*/
function prefix_the_terms( $post_id, $taxonomy ) {
$term_ids = wp_get_post_terms( $post_id, $taxonomy, array(
'fields' => 'ids',
) );
$terms = get_terms( $taxonomy, array(
'include' => $term_ids,
'orderby' => 'term_order',
'order' => 'ASC',
'hide_empty' => false
) );
$links = array();
foreach ( $terms as $term ) {
$link = $link = get_term_link( $term, $taxonomy );
$links[] = sprintf(
'<a href="%s" rel="tag">%s</a>',
esc_attr( $link ),
esc_html( $term->name )
);
}
echo implode( ', ', $links );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment