Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Created March 4, 2012 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BronsonQuick/1971062 to your computer and use it in GitHub Desktop.
Save BronsonQuick/1971062 to your computer and use it in GitHub Desktop.
A WordPress widget to generate random Testimonials from the Testimonials Custom Post Type
<?php if( ! class_exists( 'Testimonials_Widget' ) ) :
/**
* Create a widget to display a random testimonial post.
* Refer to https://gist.github.com/1971046 and https://gist.github.com/1971054 for the Custom Post Type and Meta Box Setup
*
* @since 1.0
*/
class Testimonials_Widget extends WP_Widget {
function Testimonials_Widget() {
$widget_ops = array( 'description' => __( 'Displays a random testimonial.' ), 'classname' => 'testimonials_widget' );
$this->WP_Widget( 'testimonials_widget', __( 'Testimonial' ), $widget_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = ( $instance['title'] != '' ) ? esc_attr( $instance['title'] ) : __( 'Testimonials' );
echo $before_widget;
echo $before_title . $title . $after_title;
$testimonial = new WP_Query( array( 'post_type' => 'testimonial', 'post_status' => 'publish', 'orderby' => 'rand', 'showposts' => 1 ) );
if ( $testimonial->have_posts() ) : while ( $testimonial->have_posts() ) : $testimonial->the_post();
$details[] = ucfirst( get_post_meta( get_the_ID(), 'parent_name', true ) );
$details[] = ucfirst( get_post_meta( get_the_ID(), 'child_name', true ) ) . "'s " . get_post_meta( get_the_ID(), 'parent_type', true );
$details[] = ucfirst( get_post_meta( get_the_ID(), 'parent_location', true ) );
echo '<blockquote>';
the_content();
echo '</blockquote>';
echo '<p id="testimonial_details"><strong>';
echo implode( ', ', $details );
echo '</strong></p>';
endwhile; endif;
echo $after_widget;
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Testimonials' );
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<?php
}
function update( $new_instance, $old_instance ){
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
}
endif;?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment