__('Popular Posts', LANGUAGE_ZONE) );
parent::__construct(
'dt-popular_posts-widget',
DT_WIDGET_PREFIX . __('Popular Posts', LANGUAGE_ZONE),
$widget_ops
);
}
/* Display the widget */
function widget( $args, $instance ) {
extract( $args );
/* Our variables from the widget settings. */
$defaults = array(
'title' => '',
'order' => 'ASC',
'show' => 6,
'orderby' => 'modified',
'select' => 'all',
'thumb' => true,
'cats' => array()
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = apply_filters('widget_title', $instance['title'] );
$args = array(
'no_found_rows' => 1,
'posts_per_page' => intval($instance['show']),
'post_status' => 'publish',
'orderby' => $instance['orderby'],
'order' => $instance['order'],
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $instance['cats']
) )
);
switch( $instance['select'] ) {
case 'only': $args['tax_query'][0]['operator'] = 'IN'; break;
case 'except': $args['tax_query'][0]['operator'] = 'NOT IN'; break;
default: unset( $args['tax_query'] );
}
add_filter('posts_clauses', 'dt_core_join_left_filter');
$query = new WP_Query( $args );
remove_filter('posts_clauses', 'dt_core_join_left_filter');
// caching!
if ( $query->have_posts() ) {
// get posts thumbnails id's
$images = array();
foreach ( $query->posts as $p ) {
if ( has_post_thumbnail( $p->ID ) ) {
$images[] = get_post_thumbnail_id( $p->ID );
}
}
// get all images in once
if ( $images ) {
$i_query = new WP_Query( array(
'no_found_rows' => 1,
'posts_per_page' => -1,
'post_type' => 'attachment',
'post_status' => 'inherit',
'post__in' => $images
) );
}
}
if ( $query->have_posts() ):
echo $before_widget ;
// start
if( $title ) echo $before_title . $title . $after_title;
$count = 0;
while( $query->have_posts() ): $query->the_post(); $count++;
$img = '';
if( $instance['thumb'] && has_post_thumbnail() ) {
$img_id = get_post_thumbnail_id();
// get alt
$img_alt = get_post_meta( $img_id, '_wp_attachment_image_alt', true );
if ( !$img_alt ) { $img_alt = get_the_title(); }
$img = dt_get_thumb_img( array(
'alt' => $img_alt,
'img_meta' => wp_get_attachment_image_src( $img_id, 'full' ),
'href' => get_permalink(),
'thumb_opts' => array( 'w' => 50, 'h' => 50 )
),
'',
false
);
}
?>
1,
'hierarchical' => false
) );
if( !is_wp_error($terms) ): ?>
/>