Skip to content

Snippet: Add A Single Testimonial Shortcode

By default the Total theme doesn't include a single testimonial shortcode but if you want to add one to your child theme it's very easy to do. Simply copy and paste this code into your child theme. You can tweak it for any post type.

// Add a single testimonial shortcode
// Usage: [single_testimonial id="128"]
function myprefix_single_testimonial_shortcode( $atts ) {
	ob_start();
	extract( shortcode_atts( array(
		'id' => '',
	), $atts, 'single_testimonial' ) );
	if ( $id ) {
		$wpex_query = new WP_Query( array(
			'post_type'     => 'testimonials',
			'post__in'      => array( $id ),
			'no_found_rows' => true,
		) );
		if ( $wpex_query->have_posts() ) :
			while ( $wpex_query->have_posts() ) : $wpex_query->the_post();
			if ( $template = locate_template( 'partials/testimonials/testimonials-entry.php', false ) ) {
				include( $template );
			}
			endwhile;
		endif;
		wp_reset_postdata();
	}
	return ob_get_clean();
}
add_shortcode( 'single_testimonial', 'myprefix_single_testimonial_shortcode' );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top