Skip to content

Instantly share code, notes, and snippets.

@shizhua
Created August 25, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shizhua/547bbb196289a2116889 to your computer and use it in GitHub Desktop.
Save shizhua/547bbb196289a2116889 to your computer and use it in GitHub Desktop.
Add a Fancy Testimonial to your wordpress website
jQuery(document).ready(function($) {
var e = $(".mts-testimonial"),
t = $(".testimonials-authors li"),
f = $(".mts-testimonial:first"),
l = $(".testimonials-authors li:first");
f.css({
opacity: 1
});
l.addClass("active-testimonial");
t.hover(function() {
var t = $(this),
n = "active-testimonial",
r = t.index(),
i = t.siblings("." + n).index();
if (t.hasClass(n)) return;
t.siblings("li").removeClass(n).end().addClass(n);
e.eq(i).stop(true, true).animate({
opacity: 0
}, 100, function() {
$(this).hide();
e.eq(r).css({
display: "block",
opacity: 0
}).animate({
opacity: 1
}, 100)
})
})
});
/*
* enqueue scripts
*/
add_action('wp_enqueue_scripts', 'ct_testimonial_scripts', 20, 1);
function ct_testimonial_scripts(){
wp_enqueue_style( 'ct_testimonial', 'path/to/testimonial.css', array(), CT_TESTIMONIAL__VERSION );
wp_enqueue_script( 'ct_testimonial', 'path/to/testimonial.js', array('jquery'), CT_TESTIMONIAL__VERSION, true );
}
<?php
/* Add a Fancy Testimonial to your wordpress website
* by Leo
* URL: http://wpsites.org?p=10495
*/
define( 'CT_TESTIMONIAL__VERSION', '1.0' );
/*
* Register Testimonial Post Type
*/
function ct_testimonial_posttype() {
$labels = array(
'name' => _x( 'Testimonials', 'Post Type General Name', 'clonetemplates' ),
'singular_name' => _x( 'Testimonial', 'Post Type Singular Name', 'clonetemplates' ),
'menu_name' => __( 'Testimonial', 'clonetemplates' ),
'parent_item_colon' => __( 'Parent Testimonial:', 'clonetemplates' ),
'all_items' => __( 'All Testimonials', 'clonetemplates' ),
'view_item' => __( 'View Testimonial', 'clonetemplates' ),
'add_new_item' => __( 'Add New Testimonial', 'clonetemplates' ),
'add_new' => __( 'Add New', 'clonetemplates' ),
'edit_item' => __( 'Edit Testimonial', 'clonetemplates' ),
'update_item' => __( 'Update Testimonial', 'clonetemplates' ),
'search_items' => __( 'Search Testimonial', 'clonetemplates' ),
'not_found' => __( 'Not found', 'clonetemplates' ),
'not_found_in_trash' => __( 'Not found in Trash', 'clonetemplates' ),
);
$args = array(
'label' => __( 'testimonial', 'clonetemplates' ),
'description' => __( 'A custom post type for Testimonial', 'clonetemplates' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', ),
'taxonomies' => array( 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-star-filled',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'testimonial', $args );
}
// Hook into the 'init' action
add_action( 'init', 'ct_testimonial_posttype', 0 );
/*
* Testimonial function
*/
function ct_testimonials_query( $count="" ) {
if( !$count ) $count = 10;
$content = '';
$ct_testimonial = get_transient( 'ct_testimonial' );
if ( false === $ct_testimonial ) {
$ct_testimonial_args = array(
'post_type' => 'testimonial',
'orderby' => 'date',
'posts_per_page' => $count,
'ignore_sticky_posts' => 1,
'no_found_rows' => true
);
$ct_testimonial = new WP_Query( $ct_testimonial_args );
set_transient( 'ct_testimonial', $ct_testimonial, 240 * HOUR_IN_SECONDS );
}
if ( $ct_testimonial->have_posts() ) :
$content .= '<div class="testimonials">';?>
<?php while ( $ct_testimonial->have_posts() ) : $ct_testimonial->the_post();
$content .= '<div class="mts-testimonial" id="testimonial-' . get_the_ID() . '">
<p>' . get_the_content() .'
<span class="testi-author">' . get_the_title() .'</span>
</p>
</div>';
endwhile;
$content .= '</div>';
$content .= '<ul id="testimonials-authors" class="testimonials-authors">';
while ( $ct_testimonial->have_posts() ) : $ct_testimonial->the_post();
$customer_avatar = get_the_post_thumbnail(get_the_ID(),'thumbnail');
if ( !has_post_thumbnail() ) $customer_avatar = '<img src="'.path/to/default-user-avatar.png.'" width="100" height="100" alt="'.get_the_title().'">';
$content .= '<li id="testimonial-' . get_the_ID() . '">' . $customer_avatar . '</li>';
endwhile;
$content .= '</ul>';
endif; wp_reset_query();
return $content;
}
/**
* Flush out the transients used in Testimonial.
*
*/
function ct_testimonial_transient_flusher($post) {
$slug = 'testimonial';
// If this isn't a 'testimonial' post, don't update it.
if ( $slug != $post->post_type ) {
return;
}
// Like, beat it. Dig?
delete_transient( 'ct_testimonial' );
}
add_action( 'save_post','ct_testimonial_transient_flusher' );
/*
* Testimonial Shortcode
*/
add_shortcode( 'ct_testimonial', 'ct_testimonial_shortcode' );
function ct_testimonial_shortcode( $atts ) {
$a = shortcode_atts( array(
'number' => '10',
), $atts );
$c = $a['number'];
return ct_testimonials_query($c);
}
?>
ct_testimonials_query($count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment