Last active
December 27, 2020 20:21
-
-
Save ofyalcin/123a87753d16011c15467a66d97bca0d to your computer and use it in GitHub Desktop.
Wordpress customizable post terms list generator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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