Created
April 30, 2015 05:21
-
-
Save webmasterninjay/aab3a9e91eb3eed467ef to your computer and use it in GitHub Desktop.
Wordpress: Widget to display custom post type testimonial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* START WIDGET | |
class Testimonial_Widget extends WP_Widget { | |
function __construct() { | |
parent::__construct( | |
'testimonial_widget', // Base ID | |
__('Testimonial Widgets', 'marilynhorowitz'), // Name | |
array( 'description' => __( 'Widget to display testimonials', 'marilynhorowitz' ), ) // Args | |
); | |
} | |
//* FRONTEND WIDGET DISPLAY | |
public function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters('widget_title', $instance['title']); | |
$NumberOfTestimonial = $instance['NumberOfTestimonial']; | |
echo $before_widget; | |
if ( $title ) { | |
echo $before_title . $title . $after_title; | |
} | |
$this->getTestimonials($NumberOfTestimonial); | |
echo $after_widget; | |
} | |
//* BACKEND WIDGET FORM | |
public function form( $instance ) { | |
if ($instance) { | |
$title = esc_attr($instance['title']); | |
$NumberOfTestimonial = esc_attr($instance['NumberOfTestimonial']); | |
} | |
else { | |
$title = ''; | |
$NumberOfTestimonial = ''; | |
} | |
?> | |
<!-- WIDGET FORM --> | |
<p> | |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'marilynhorowitz'); ?></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> | |
<p> | |
<label for="<?php echo $this->get_field_id('NumberOfTestimonial'); ?>"><?php _e('Number of Testimonial:', 'marilynhorowitz'); ?></label> | |
<select id="<?php echo $this->get_field_id('NumberOfTestimonial'); ?>" name="<?php echo $this->get_field_name('NumberOfTestimonial'); ?>"> | |
<?php for($x=1;$x<=10;$x++): ?> | |
<option <?php echo $x == $NumberOfTestimonial ? 'selected="selected"' : '';?> value="<?php echo $x;?>"><?php echo $x; ?></option> | |
<?php endfor;?> | |
</select> | |
</p> | |
<!-- END OF WIDGET FORM --> | |
<?php | |
} | |
//* UPDATE THE DATA | |
public function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
$instance['NumberOfTestimonial'] = strip_tags($new_instance['NumberOfTestimonial']); | |
return $instance; | |
} | |
//* QUERY THE TESTIMONIALS POST TYPE | |
function getTestimonials($NumberOfTestimonial) { | |
global $post; | |
$args = array( | |
'post_type' => 'testimonial_type', | |
'showposts' => $NumberOfTestimonial, | |
); | |
$tlistings = new WP_Query( $args ); | |
if ( $tlistings->have_posts() ) { | |
echo '<ul>'; | |
while ( $tlistings->have_posts() ) { | |
$tlistings->the_post(); | |
echo '<li>'; | |
echo '<blockquote>' . get_the_excerpt(). '</blockquote>'; | |
echo '<p>' . get_the_title(). '</p>'; | |
echo '</li>'; | |
} | |
echo '<ul>'; | |
} | |
else { | |
echo '<p>No Testimonial yet</p>'; | |
} | |
//* NOW WE NEED TO RESET WP POST DATA | |
wp_reset_postdata(); | |
// END OF getTestimonials | |
} | |
} | |
//* REGISTER THE WIDGET | |
function register_testimonial_widget() { | |
register_widget( 'Testimonial_Widget' ); | |
} | |
add_action( 'widgets_init', 'register_testimonial_widget' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment