Skip to content

Instantly share code, notes, and snippets.

@koganas
Last active October 29, 2019 18:02
Show Gist options
  • Save koganas/13a2c3e1eb82a4f4d6183723baed50ea to your computer and use it in GitHub Desktop.
Save koganas/13a2c3e1eb82a4f4d6183723baed50ea to your computer and use it in GitHub Desktop.
Wordpress category functions using YOAST
function get_primary_category() {
$category = get_the_category();
$useCatLink = true;
/* if( in_array( 'wordpress-seo/wp-seo.php', apply_filters( 'active_plugins', get_option('active_plugins') ) ) ) */
if ($category) {
$category_display = '';
$category_link = '';
if ( class_exists('WPSEO_Primary_Term') ) {
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if (is_wp_error($term)) {
$category_display = $category[0]->name;
$category_link = get_category_link( $category[0]->term_id );
} else {
$category_display = $term->name;
$category_link = get_category_link( $term->term_id );
}
} else {
$category_display = $category[0]->name;
$category_link = get_category_link( $category[0]->term_id );
}
if ( !empty($category_display) ){
if ( $useCatLink == true && !empty($category_link) ){
//echo '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
return htmlspecialchars($category_display);
} else {
return htmlspecialchars($category_display);
}
}
}
}
// Display primary category slug for redirection
function get_primary_category_slug() {
$category = get_the_category();
$useCatLink = true;
if ($category) {
$category_display = '';
$category_link = '';
if ( class_exists('WPSEO_Primary_Term') ) {
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if (is_wp_error($term)) {
$category_display = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
} else {
$category_display = $term->slug;
$category_link = get_category_link( $term->term_id );
}
} else {
$category_display = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
}
if ( !empty($category_display) ){
if ( $useCatLink == true && !empty($category_link) ){
return htmlspecialchars($category_display);
} else {
return htmlspecialchars($category_display);
}
}
}
}
/* If you are using a custom field from the primary category */
function get_primary_category_color() {
$category = get_the_category();
if ($category) {
if ( class_exists('WPSEO_Primary_Term') ) {
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
return get_field('category-color', 'term_'.$term->term_id);
} else {
return get_field('category-color', 'term_'.get_the_id());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment