/** use this function to decide how the widget will display in your theme */ public function widget($args, $instance) { if (is_single()) { extract($args); $title = apply_filters('widget_title', $instance['title']); $show_meta = $instance['meta']; $show_thumbnail = $instance['thumbnail']; $count = esc_attr($instance['count']); if (!$count) { $count = 3; } $count = (int) $count; $duration = esc_attr($instance['duration']); if (!$duration) { $duration = 1; } $duration = (int) $duration; echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // save post for backup later global $post; $origin_post = $post; /*QUERY*/ // common query $args = array('posts_per_page' => $count, 'order' => 'DESC', 'orderby' => 'rand', 'post__not_in' => array($post->ID)); // time query if ($duration == 2) { //1 year ago $args = wp_parse_args($args, array('date_query' => array(array('column' => 'post_date_gmt', 'before' => '1 year ago')))); } else { if ($duration == 3) { //1 month ago $args = wp_parse_args($args, array('date_query' => array(array('column' => 'post_date_gmt', 'before' => '1 month ago')))); } else { if ($duration == 4) { //1 week ago $args = wp_parse_args($args, array('date_query' => array(array('column' => 'post_date_gmt', 'before' => '1 week ago')))); } } } $tags = wp_get_post_tags($post->ID); if ($tags) { /*Get post from tags if post had at least 1 tag*/ $tag_ids = array(); foreach ($tags as $tag) { $tag_ids[] = $tag->term_id; } $args = wp_parse_args($args, array('tag__in' => $tag_ids)); } else { $cat_ids = wp_get_post_categories($post->ID); if ($cat_ids) { /*in case empty tags, get from cate*/ $args = wp_parse_args($args, array('category__in ' => $cat_ids)); } /* else: just random posts */ } $my_query = new WP_Query($args); // Show HTML if ($my_query->have_posts()) { echo '<div class="news-box three related-news"><div class="outer"><ul class="content">'; $counter = 0; while ($my_query->have_posts()) { $my_query->the_post(); // preparing for flexible home layout output $item_img_medium = '<a href="' . get_permalink() . '" title="' . __('Click to read', THEME_DOMAIN) . '" class="item-thumbnail">' . get_post_image(get_the_ID(), 'medium', array('alt' => 'item-thumbnail', 'title' => esc_attr(get_the_title()))) . '</a>'; $item_title = '<h3 class="title"><a href="' . get_permalink() . '" title="' . __('Click to read', THEME_DOMAIN) . '">' . get_the_title() . '</a></h3>'; $item_snip = get_the_snippet(); $item_meta = '<div class="meta">'; if (comments_open()) { $item_meta .= '<a href="' . get_comments_link() . '" class="comment"><i class="icon"></i><span>' . get_comments_number() . '</span></a>'; } $item_meta .= '<a href="' . get_permalink() . '" class="date"><i class="icon"></i><span>' . get_the_date() . '</span></a>'; $item_meta .= '</div>'; echo '<li class="item item-' . $counter . ' item-' . ($counter == 0 ? 'first' : 'other') . '"><div class="inner">'; echo ($show_thumbnail ? $item_img_medium : '') . ($show_meta ? $item_meta : '') . $item_title; echo '<div class="clear"></div></div></li>'; $counter++; } echo '</ul></div><div class="clear"></div></div>'; } wp_reset_postdata(); echo $after_widget; $post = $origin_post; } // is_single }
echo '</div>'; } ?> <a href="<?php echo get_permalink(); ?> " class="date"><i class="icon"></i><span><?php echo get_the_date(); ?> </span></a> </div><!-- .post-meta --> <div class="post-body post-body-archive"> <?php echo get_the_snippet(); ?> </div><!-- .post-body --> <div class="clear"></div> </div><!-- .archive-inner --> </article><!-- #post --> <?php } /*end have_posts*/ ?> <?php get_template_part('content-pagination');