• Resolved rawhasan

    (@rawhasan)


    Hi Chris,

    Need a little more help!

    On my customized single.php page for the testimonials, I am trying to show some related testimonials in the end of the page based on the testimonial category of the current testimonial which is showing. Can’t figure out how to make the condition to check the testimonial category of the current testimonial.

    Currently I am just showing one single view with some recent testimonials without any condition:

    <?php echo do_shortcode( '[testimonial_view id=4]' ); ?>

    But I want to show different views based on the category of the current testimonial showing.

    Please help!

    Regards,
    Hasan

    • This topic was modified 7 years, 10 months ago by rawhasan.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Chris Dillon

    (@cdillon27)

    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
    Thread Starter rawhasan

    (@rawhasan)

    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

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Displaying a view based on category’ is closed to new replies.