Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Post Grid - Yoast Primary Category Support
<?php
/*
Plugin Name: Post Grid Yoast Primary Category
Plugin URI: https://runthings.dev/
Description: Bring the primary category to the start of the list
Version: 1.0.0
Author: runthings.dev
Author URI: https://runthings.dev/
*/
if ( ! defined('ABSPATH')) exit; // if direct access
add_action( 'plugins_loaded', 'rtp_category_override' );
function rtp_category_override() {
remove_action('post_grid_layout_element_categories', 'post_grid_layout_element_categories');
add_action('post_grid_layout_element_categories', 'rtp_post_grid_layout_element_categories');
}
function rtp_post_grid_layout_element_categories($args){
$element = isset($args['element']) ? $args['element'] : array();
$elementIndex = isset($args['index']) ? $args['index'] : '';
$post_id = isset($args['post_id']) ? $args['post_id'] : '';
if(empty($post_id)) return;
$custom_class = isset($element['custom_class']) ? $element['custom_class'] : '';
$custom_class = do_shortcode($custom_class);
$link_target = isset($element['link_target']) ? $element['link_target'] : '';
$max_count = isset($element['max_count']) ? (int) $element['max_count'] : 3;
$wrapper_html = !empty($element['wrapper_html']) ? $element['wrapper_html'] : '%s';
$separator = isset($element['separator']) ? $element['separator'] : ', ';
$term_list = rtp_get_categories_list($post_id);
$categories_html = '';
$term_total_count = count($term_list);
$max_term_limit = ($term_total_count < $max_count) ? $term_total_count : $max_count ;
$i = 0;
foreach ($term_list as $term){
if($i >= $max_count) continue;
$term_id = isset($term->term_id) ? $term->term_id : '';
$term_name = isset($term->name) ? $term->name : '';
$term_link = get_term_link($term_id);
$categories_html .= '<a target="'.esc_attr($link_target).'" href="'.esc_url($term_link).'">'.esc_html($term_name).'</a>';
if( $i+1 < $max_term_limit){ $categories_html .= $separator;}
$i++;
}
//var_dump($categories_html);
?>
<div class="element element_<?php echo esc_attr($elementIndex); ?> <?php echo esc_attr($custom_class); ?> categories ">
<?php echo sprintf($wrapper_html, $categories_html); ?>
</div>
<?php
}
/**
* Get the categories, and reorder if Yoast plugin is installed, to put
* primary category first
*
* @source https://www.lab21.gr/blog/wordpress-get-primary-category-post/
*/
function rtp_get_categories_list($post_id)
{
$term = "category";
$all_terms = wp_get_post_terms( $post_id, $term, array( 'fields' => 'all' ) );
// default behaviour if Yoast not installed
if (!class_exists('WPSEO_Primary_Term')){
return $all_terms;
}
// try to get Yoast primary category
$wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id );
$primary_term = get_term($wpseo_primary_term->get_primary_term());
if (is_wp_error($primary_term)){
return $all_terms;
}
// bring primary term to start of array
foreach ($all_terms as $key => $object) {
if ($object->term_id === $primary_term->term_id) {
// remove it from its current location
array_splice($all_terms, $key, 1);
// add primary term to start of array
array_unshift($all_terms, $primary_term);
break;
}
}
return $all_terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment