Great idea!
Add this to your theme functions.php:
/**
* Return a comma-separated list of other posts in the same category as the current post.
*/
function my_related_testimonials( $post_id ) {
// Create an empty list.
$post_ids = '';
// Get the terms assigned to this post.
$terms = get_the_terms( $post_id, 'wpm-testimonial-category' );
// Check the results.
if ( $terms && ! is_wp_error( $terms ) ) {
// Extract just the term IDs.
$term_ids = wp_list_pluck( $terms, 'term_id' );
// Get other posts in this taxonomy.
$posts = get_posts( array(
'post_type' => 'wpm-testimonial',
'post_status' => 'publish',
'posts_per_page' => -1,
'exclude' => $post_id,
'tax_query' => array(
array(
'taxonomy' => 'wpm-testimonial-category',
'field' => 'term_id',
'terms' => $term_ids,
),
),
) );
// Extract just the post IDs and assemble into a comma-separated list.
$post_ids = implode( ',', wp_list_pluck( $posts, 'ID' ) );
}
return $post_ids;
}
Use this in your single template:
<?php
if ( function_exists( 'my_related_testimonials' ) ) {
$post_ids = my_related_testimonials( get_the_ID() );
} else {
$post_ids = '';
}
echo do_shortcode( '[testimonial_view id="4" post_ids="' . $post_ids . '"]' );
?>
-
This reply was modified 7 years, 10 months ago by
Chris Dillon. Reason: Fixed view id number
Worked like charm! It even excludes the current testimonial from the related list and avoid duplicates – awesome!!
Thanks buddy for rescuing me! I can now say, I have the best implementation of your awesome plugin for our industry!
I’ll look at your add-on options now to purchase and support this awesome plug-in. Please never stop developing it!
Regards,
Hasan