Skip to content

Instantly share code, notes, and snippets.

@isGabe
Last active February 12, 2022 16:22
Show Gist options
  • Save isGabe/4184527 to your computer and use it in GitHub Desktop.
Save isGabe/4184527 to your computer and use it in GitHub Desktop.
WordPress: Output a list of taxonomy terms, then highlight the terms the belong to the current post. Updated to use in_array() to add a "current" CSS class to terms that the current post has. This way we don't have to worry about keeping up with terms in the CSS. Hooray for logic! #snippet #WordPress
<?php
/*
As the title implies, this will give you a way to
1. output a complete list of terms for a given taxonomy (nothing special there)
2. highlight the terms that the current post has (the magic!)
Probably need to figure out a better way to echo out the url of the term archive page...
*/
$post_terms = get_terms('custom_taxonomy'); // get all terms in taxonomy
$post_current_terms = wp_get_post_terms( $post->ID, 'custom_taxonomy', array('fields' => 'slugs') ); // get terms current post has
?>
<footer class="custom-taxonomy">
<?php foreach( $post_terms as $post_term ) :
$post_term_slug = $post_term->slug;
?>
<a <?php if ( in_array($post_term_slug, $post_current_terms) ) { echo 'class="current"'; } ?> href="<?php echo site_url() . '/taxonomy-slug/'. $post_term->slug; ?>"><?php echo $post_term->name; ?></a>
<?php endforeach; ?>
</footer>
<?php
/* The same idea as above, but put into an action that can be hooked into */
// adding the action
add_action('after_single_project_content', 'fstop_highlight_terms', 10);
// creating the function
function fstop_highlight_terms(){
// make sure we're getting current the post loop data
global $post;
// get all the terms in the given taxonomy
$post_terms = get_terms('custom_taxonomy');
// get only the given taxonomy terms assigned to the current post
$post_current_terms = wp_get_post_terms( $post->ID, 'custom_taxonomy', array('fields' => 'slugs') );
// start the taxonomy term output
echo '<footer class="taxonomy-slug">';
// loop through all givent taxonomy terms
foreach( $post_terms as $post_term ) :
// set a variable equal to the individual term slug, which we'll use to check against the current post's terms
$post_term_slug = $post_term->slug;
//start echoing out the terms as links
echo '<a '; // need the space after to separate it from the class
// check each term against the current post's assigned terms, if there's a match, add 'class="current" '
if ( in_array($post_term_slug, $post_current_terms) ) {
echo 'class="current" '; // need the space after to separate it from the href
}
// echo out the rest of the link to the term archive page. There may be a better way to do this...
echo 'href="' . site_url() . '/taxonomy-slug/' . $post_term->slug . '">' . $post_term->name . '</a>';
endforeach;
echo '</footer>';
// profit?
}
// put a do_action() in your template files wherever you want this to output
do_action('after_single_project_content');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment