Skip to content

Instantly share code, notes, and snippets.

@ofyalcin
Last active December 27, 2020 20:21
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 ofyalcin/123a87753d16011c15467a66d97bca0d to your computer and use it in GitHub Desktop.
Save ofyalcin/123a87753d16011c15467a66d97bca0d to your computer and use it in GitHub Desktop.
Wordpress customizable post terms list generator.
<?php
/**
* Wordpress customizable post terms list generator
*
* Add to your theme functions.php file.
* @param null $post_id
* @param string $tax
* @param bool $with_link
* @param string $seperator
* @param null $before
* @param null $after
* @return string|void
*/
function bkthemes_term_list_generator ( $post_id = null, $tax = 'category', $with_link = true, $seperator = ', ', $before = null, $after = null ){
if (empty($post_id)) return;
$output = '';
$terms = wp_get_post_terms( $post_id, $tax );
if ( !empty($terms) ){
$i = 0;
$count = count($terms);
$output .= $before;
foreach ($terms as $term) {
$i++;
if ( $with_link ){
$output .= '<a href="'.get_term_link($term->term_id).'" class="term-list-item">'.$term->name.'</a>';
} else {
$output .= $term->name;
}
if ( $i < count($terms) ) $output .= $seperator;
}
$output .= $after;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment