Created
February 3, 2013 01:42
-
-
Save eugenoprea/4700177 to your computer and use it in GitHub Desktop.
Genesis - How to Display Custom Taxonomy Terms - Helper Function
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
/** | |
* Returns the given number of terms from the specified taxonomies (for the current post). | |
* | |
* @param (string|array) $taxonomies The taxonomy(es) to retrieve terms from | |
* @param (int) $term_number Limit the number of terms returned (-1 for no limit) | |
* @param (bool) $term_links Whether to return links to term archives or not | |
* @param (string) $separator String that will be used as the terms separarator | |
* @return (string) The list of taxonomy terms for the current post or empty string | |
*/ | |
function eo_get_tax_terms($taxonomies, $term_number = -1, $term_links = true, $separator = ', ') | |
{ | |
global $post; | |
// Function: wp_get_object_terms($object_ids, $taxonomies, $args) | |
$args = array('orderby' => 'count', 'order' => 'DESC', 'fields' => 'all'); | |
$terms = wp_get_object_terms($post->ID, $taxonomies, $args); | |
if ( empty($terms) || is_wp_error($terms) ) | |
return ''; | |
$ret = ''; | |
$count = 0; | |
foreach ($terms as $term) | |
{ | |
if ($count == $term_number) | |
break; | |
$count++; | |
if ( ! empty($ret) ) | |
$ret .= $separator; | |
$ret .= $term_links ? | |
('<a href="' . get_term_link($term) . '">' . esc_html($term->name) . '</a>') : | |
esc_html($term->name); | |
} | |
return $ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment