Skip to content

Instantly share code, notes, and snippets.

@thanh4890
Created April 7, 2014 06:53
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 thanh4890/10015800 to your computer and use it in GitHub Desktop.
Save thanh4890/10015800 to your computer and use it in GitHub Desktop.
/**
* Display testimonials widget
* @since Nictitate 1.0
*/
class Kopa_Widget_Staffs extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'kopa-our-team-widget', 'description' => __('Display a staffs widget', kopa_get_domain()));
$control_ops = array('width' => 'auto', 'height' => 'auto');
parent::__construct('kopa_widget_staffs', __('Kopa Staffs Widget', kopa_get_domain()), $widget_ops, $control_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$query_args['post_type'] = 'staffs';
$query_args['cat_name'] = 'staff_category';
$query_args['tag_name'] = 'staff_tag';
$query_args['categories'] = $instance['categories'];
$query_args['relation'] = esc_attr($instance['relation']);
$query_args['tags'] = $instance['tags'];
$query_args['posts_per_page'] = (int) $instance['posts_per_page'];
$query_args['orderby'] = $instance['orderby'];
$staffs = kopa_widget_posttype_build_query($query_args);
echo $before_widget;
if ( $staffs->post_count == 0 ) {
echo $after_widget;
return;
}
if ( ! empty( $title ) )
echo $before_title . '<span data-icon="&#xe014;"></span>' . $title . $after_title;
?>
<ul class="clearfix">
<?php
$staff_index = 1;
while ( $staffs->have_posts() ) : $staffs->the_post();
$thumbnail_id = get_post_thumbnail_id();
$thumbnail = wp_get_attachment_image_src( $thumbnail_id, 'kopa-image-size-3' );
$staff_position = get_post_meta( get_the_ID(), 'position', true );
$staff_facebook = get_post_meta( get_the_ID(), 'facebook', true );
$staff_twitter = get_post_meta( get_the_ID(), 'twitter', true );
$staff_gplus = get_post_meta( get_the_ID(), 'gplus', true );
$staff_email = get_post_meta( get_the_ID(), 'email', true );
?>
<li>
<article class="entry-item clearfix">
<div class="entry-thumb">
<img src="<?php echo $thumbnail[0]; ?>" alt="<?php the_title(); ?>">
</div>
<div class="entry-content">
<header>
<h6 class="entry-title"><?php the_title(); ?><span></span></h6>
<span><?php echo $staff_position; ?></span>
</header>
<?php the_excerpt(); ?>
<ul class="our-team-social-link clearfix">
<?php if ( ! empty( $staff_facebook ) ) : ?>
<li><a href="<?php echo esc_url($staff_facebook); ?>" data-icon="&#xe168;"></a></li>
<?php endif; ?>
<?php if ( ! empty( $staff_twitter ) ) : ?>
<li><a href="<?php echo esc_url($staff_twitter); ?>" data-icon="&#xe16c;"></a></li>
<?php endif; ?>
<?php if ( ! empty( $staff_gplus ) ) : ?>
<li><a href="<?php echo esc_url($staff_gplus); ?>" data-icon="&#xe163;"></a></li>
<?php endif; ?>
<?php if ( ! empty( $staff_email ) && is_email( $staff_email ) ) : ?>
<li><a href="mailto:<?php echo esc_attr($staff_email); ?>" data-icon="&#xe272;"></a></li>
<?php endif; ?>
</ul>
</div>
</article>
</li>
<?php
if ( $staff_index % 4 == 0 && $staff_index != $staffs->post_count )
echo '</ul><ul class="clearfix mt-20">';
$staff_index++;
endwhile; ?>
</ul>
<?php wp_reset_postdata();
echo $after_widget;
}
function form($instance) {
$default = array(
'title' => __( 'Our Team', kopa_get_domain() ),
'categories' => array(),
'relation' => 'OR',
'tags' => array(),
'posts_per_page' => 4,
'orderby' => 'lastest'
);
$instance = wp_parse_args((array) $instance, $default);
$title = strip_tags($instance['title']);
$form['categories'] = $instance['categories'];
$form['relation'] = esc_attr($instance['relation']);
$form['tags'] = $instance['tags'];
$form['posts_per_page'] = (int) $instance['posts_per_page'];
$form['orderby'] = $instance['orderby'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', kopa_get_domain()); ?></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 esc_attr($title); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('categories'); ?>"><?php _e('Categories:', kopa_get_domain()); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>[]" multiple="multiple" size="5" autocomplete="off">
<option value=""><?php _e('-- None --', kopa_get_domain()); ?></option>
<?php
$categories = get_terms('staff_category');
foreach ($categories as $category) {
printf('<option value="%1$s" %4$s>%2$s (%3$s)</option>', $category->term_id, $category->name, $category->count, (in_array($category->term_id, (isset($form['categories']) ? $form['categories'] : array()))) ? 'selected="selected"' : '');
}
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('relation'); ?>"><?php _e('Relation:', kopa_get_domain()); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('relation'); ?>" name="<?php echo $this->get_field_name('relation'); ?>" autocomplete="off">
<?php
$relation = array(
'AND' => __('And', kopa_get_domain()),
'OR' => __('Or', kopa_get_domain())
);
foreach ($relation as $value => $title) {
printf('<option value="%1$s" %3$s>%2$s</option>', $value, $title, ($value === $form['relation']) ? 'selected="selected"' : '');
}
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('tags'); ?>"><?php _e('Tags:', kopa_get_domain()); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('tags'); ?>" name="<?php echo $this->get_field_name('tags'); ?>[]" multiple="multiple" size="5" autocomplete="off">
<option value=""><?php _e('-- None --', kopa_get_domain()); ?></option>
<?php
$tags = get_terms('staff_tag');
foreach ($tags as $tag) {
printf('<option value="%1$s" %4$s>%2$s (%3$s)</option>', $tag->term_id, $tag->name, $tag->count, (in_array($tag->term_id, (isset($form['tags']) ? $form['tags'] : array()))) ? 'selected="selected"' : '');
}
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('posts_per_page'); ?>"><?php _e('Number of items:', kopa_get_domain()); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('posts_per_page'); ?>" name="<?php echo $this->get_field_name('posts_per_page'); ?>" value="<?php echo $form['posts_per_page']; ?>" type="number" min="1">
</p>
<p>
<label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e('Orderby:', kopa_get_domain()); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>" autocomplete="off">
<?php
$orderby = array(
'lastest' => __('Latest', kopa_get_domain()),
'random' => __('Random', kopa_get_domain()),
);
foreach ($orderby as $value => $title) {
printf('<option value="%1$s" %3$s>%2$s</option>', $value, $title, ($value === $form['orderby']) ? 'selected="selected"' : '');
}
?>
</select>
</p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['categories'] = (empty($new_instance['categories'])) ? array() : array_filter($new_instance['categories']);
$instance['relation'] = $new_instance['relation'];
$instance['tags'] = (empty($new_instance['tags'])) ? array() : array_filter($new_instance['tags']);
$instance['posts_per_page'] = (int) $new_instance['posts_per_page'] ? (int) $new_instance['posts_per_page'] : 16;
$instance['orderby'] = $new_instance['orderby'];
$instance['title_position'] = (int) $new_instance['title_position'] ? (int) $new_instance['title_position'] : 1;
$instance['title_position'] = $instance['title_position'] > $instance['posts_per_page'] ?
$instance['posts_per_page'] + 1 :
$instance['title_position'];
return $instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment