Skip to content

Instantly share code, notes, and snippets.

@brianpcharles
Last active April 4, 2017 14:48
Show Gist options
  • Select an option

  • Save brianpcharles/1fa36953879bb188140b8b67f75394e5 to your computer and use it in GitHub Desktop.

Select an option

Save brianpcharles/1fa36953879bb188140b8b67f75394e5 to your computer and use it in GitHub Desktop.
Wordpress - Use Yoast and Custom Post Type to create a list of terms, primary first.
function primary_taxonomy_list($taxonomy, $post_id = 0, $delimiter = ', ') {
if(empty($post_id)) {
global $post;
if(!empty($post)) $post_id = $post->ID;
else return '';
}
$primary = '';
if ( defined('WPSEO_VERSION') ) {
$seo_terms = new WPSEO_Primary_Term($taxonomy, $post_id);
$primary = $seo_terms->get_primary_term();
$primary = get_term($primary);
}
$terms = get_the_terms( $post_id, $taxonomy );
if($terms) {
$list = [];
if(!empty($primary) && $primary instanceof WP_Term) $list[] = $primary->name;
foreach($terms as $term) {
if($term instanceof WP_Term) {
if($term == $primary) {
continue;
}
$list[] = $term->name;
}
}
return implode(', ', $list);
}
return '';
}
add_filter( 'primary-taxonomy-list', 'primary_taxonomy_list', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment