Skip to content

Instantly share code, notes, and snippets.

@alexmustin
Last active January 15, 2018 20:45
Show Gist options
  • Save alexmustin/9f46f43995a9b395faef277030ecc582 to your computer and use it in GitHub Desktop.
Save alexmustin/9f46f43995a9b395faef277030ecc582 to your computer and use it in GitHub Desktop.
Social Proof Slider - Show all testimonials in a UL list
<?php
/*
* This code will create a new 'list-testimonials' shortcode, which will show all testimonials in a UL list.
* Add this to your theme's functions.php file.
*/
// Add Shortcode
add_shortcode( 'list-testimonials', 'spslider_list_testimonials' );
// Shortcode function
function spslider_list_testimonials(){
// Set the output var
$return = '';
// WP Query arguments
$args = array(
'post_type' => array( 'socialproofslider' ),
'post_status' => array( 'published' ),
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'date',
);
// Query for WP items
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
$return .= '<ul class="testimonials-list">';
while ( $query->have_posts() ) : $query->the_post();
// Get the post ID
$postid = get_the_ID();
// Get meta values
$meta = get_post_custom( $postid );
// Get the Testimonial Text
$testimonialtext = '';
$testimonialtext = $meta['socialproofslider_testimonial_text'][0];
// Get option for Smart Quotes
$surroundWithQuotes = get_option('social_proof_slider_surroundquotes');
// Set the Smart Quotes string
$quoteTextStart = "";
$quoteTextEnd = "";
if( $surroundWithQuotes ){
$quoteTextStart = "“";
$quoteTextEnd = "”";
}
// Set default value for showing Featured Image
$showThisFeaturedImage = true; // default
// Check if Featured Image exists
if ( function_exists('has_post_thumbnail') && has_post_thumbnail( $postid ) ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $postid ), 'social-proof-slider' );
if ( !$thumb[0] ){
// no src, do nothing
$showThisFeaturedImage = false;
} else {
$theFeaturedImage = $thumb[0];
}
}
$return .= '<li class="testimonial-item item-'.$postid.'">';
// Output the Featured Image
if ( $showThisFeaturedImage ) {
$return .= '<div class="testimonial-author-img-wrap">';
$return .= '<div class="testimonial-author-img">';
$return .= '<img src="' . $theFeaturedImage . '" class="client-image" />';
$return .= '</div>';
$return .= '</div>';
}
// Output the Testimonial Text
$return .= '<div class="quote">';
$return .= $quoteTextStart . html_entity_decode( $testimonialtext ) . $quoteTextEnd;
$return .= '</div>';
$return .= '</li>';
endwhile;
$return .= '</ul>';
endif;
// Reset the query
wp_reset_postdata();
// Output everything
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment