Last active
April 2, 2019 20:40
-
-
Save verticalgrain/64689e5f6da0eab4be58530f49e283da to your computer and use it in GitHub Desktop.
Use Yoast primary category functionality to order primary category before other categories on frontend
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 | |
function get_modified_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) { | |
$terms = get_the_terms( $id, $taxonomy ); | |
if ( is_wp_error( $terms ) ) | |
return $terms; | |
if ( empty( $terms ) ) | |
return false; | |
foreach ( $terms as $term ) { | |
if(!in_array($term->term_id,$exclude)) { | |
$link = get_term_link( $term, $taxonomy ); | |
if ( is_wp_error( $link ) ) | |
return $link; | |
$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>'; | |
} | |
} | |
if( !isset( $term_links ) ) | |
return false; | |
return $before . join( $sep, $term_links ) . $after; | |
} |
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 | |
function get_primary_category( $post = 0, $taxonomy = 'category' ) { | |
if ( ! $post ) { | |
$post = get_the_ID(); | |
} | |
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY | |
$category = get_the_terms( $post, $taxonomy ); | |
$primary_category = array(); | |
// If post has a category assigned. | |
if ($category){ | |
$category_display = ''; | |
$category_slug = ''; | |
$category_link = ''; | |
$category_id = ''; | |
if ( class_exists('WPSEO_Primary_Term') ) | |
{ | |
// Show the post's 'Primary' category, if this Yoast feature is available, & one is set | |
$wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post ); | |
$wpseo_primary_term = $wpseo_primary_term->get_primary_term(); | |
$term = get_term( $wpseo_primary_term ); | |
if (is_wp_error($term)) { | |
// Default to first category (not Yoast) if an error is returned | |
$category_display = $category[0]->name; | |
$category_slug = $category[0]->slug; | |
$category_link = get_category_link( $category[0]->term_id ); | |
$category_id = $category[0]->term_id; | |
} else { | |
// Yoast Primary category | |
$category_display = $term->name; | |
$category_slug = $term->slug; | |
$category_link = get_category_link( $term->term_id ); | |
$category_id = $term->term_id; | |
} | |
} | |
else { | |
// Default, display the first category in WP's list of assigned categories | |
$category_display = $category[0]->name; | |
$category_slug = $category[0]->slug; | |
$category_link = get_category_link( $category[0]->term_id ); | |
$category_id = $term->term_id; | |
} | |
$primary_category['url'] = $category_link; | |
$primary_category['slug'] = $category_slug; | |
$primary_category['title'] = $category_display; | |
$primary_category['id'] = $category_id; | |
} | |
return $primary_category; | |
} | |
?> |
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
function project_categories() { | |
global $post; | |
// Get the primary category array | |
// If using a custom post type, replace second argument with custom post type taxonomy | |
$primary_category = get_primary_category($post->ID, 'category'); | |
// Get the categories of the post, but exclude the primary category | |
$terms = get_modified_term_list( $post->ID, 'category', '', ' / ', '', array( $primary_category[id] ) ); | |
$result = "<p class='category-list'>"; | |
$result .= "<a href='" . $primary_category[url] . "'>" . $primary_category[title] . "</a>"; | |
// If there are more than one terms, add the slash character to separate them from the primary term | |
if ( $terms != '' ) { | |
$result .= " / "; | |
$result .= $terms; | |
} | |
$result .= "</p>"; | |
return $result; | |
} | |
add_shortcode('project-categories', 'project_categories'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment