Hi burlyqlady
Sorry for the late reply.
You can set the columns to 1 to have more room for the terms. If you have many terms it could get very crouded with terms.
Here is the code translated for the widget. Choose the “Thumbnail with terms in common” option in the format dropdown.
add_filter( 'related_posts_by_taxonomy', 'rpbt_add_common_terms', 10, 4 );
function rpbt_add_common_terms( $related, $post_id, $taxonomies, $args ) {
if ( ! ( isset( $args['related_terms'] ) && $args['related_terms'] ) ) {
return $related;
}
if ( ! isset( $args['_in_common'] ) ) {
return $related;
}
// get the terms (objects) used to calculate the related posts
$related_terms = get_terms( array(
'taxonomy' => $taxonomies,
'include' => $args['related_terms'],
)
);
// Get the term names.
$names = wp_list_pluck( $related_terms, 'name', 'term_id' );
foreach ( $related as $key => $post ) {
// Get the terms from the related post.
$terms = wp_get_object_terms( $post->ID, $taxonomies, array( 'fields' => 'ids' ) );
// Get common terms from the related post as array keys.
$in_common = array_flip( array_intersect( $terms, $args['related_terms'] ) );
// Get the term names.
$in_common = array_intersect_key( $names, $in_common );
$title = count( $in_common ) . '/' . count( $terms );
$title .= ' Chords Common – ' . implode( ', ', $in_common );
$related[ $key ]->in_common = $title;
}
return $related;
}
add_filter( 'related_posts_by_taxonomy_caption', 'rpbt_add_common_terms_gallery_caption', 10, 2 );
function rpbt_add_common_terms_gallery_caption( $caption, $post ){
$caption = '<a href=' . get_permalink( $post->ID ) . '>' . $caption . '</a>';
$caption .= isset( $post->in_common ) ? ' – ' . $post->in_common : '';
return $caption;
}
add_filter( 'related_posts_by_taxonomy_template', 'rpbt_thumbnail_exerpt_format_template', 10, 3 );
// Return the right template for the thumbnail_excerpt format.
function rpbt_thumbnail_exerpt_format_template( $template, $type, $format ) {
if ( isset( $format ) && ( 'thumbnail_in_common' === $format ) ) {
return 'related-posts-thumbnails.php';
}
return $template;
}
// Create new format thumbnail_excerpt for use in widget and shortcode.
add_action( 'wp_loaded', 'rpbt_thumbnail_in_common', 11 );
function rpbt_thumbnail_in_common() {
if ( ! class_exists( 'Related_Posts_By_Taxonomy_Defaults' ) ) {
return;
}
$defaults = Related_Posts_By_Taxonomy_Defaults::get_instance();
// Add the new format .
$defaults->formats['thumbnail_in_common'] = __( 'Thumbnail with terms in common' );
}
// Return posts with post thumbnails for the thumbnail_excerpt format.
add_filter( 'related_posts_by_taxonomy_shortcode_atts', 'rpbt_thumbnail_in_common_args' ); // shortcode.
add_filter( 'related_posts_by_taxonomy_widget_args', 'rpbt_thumbnail_in_common_args' ); // widget.
function rpbt_thumbnail_in_common_args(
$args ) {
if ( 'thumbnail_in_common' === $args['format'] ) {
$args['post_thumbnail'] = true;
$args['format'] = 'thumbnails';
$args['_in_common'] = true;
}
return $args;
}