__( 'Shows most recent posts. You can customize it easily.', 'adv-recent-posts' ) ); //Create widget $this->WP_Widget( 'advancedrecentposts', __( 'Advanced Recent Posts', 'adv-recent-posts' ), $widget_ops ); } function widget( $args, $instance ) { extract( $args, EXTR_SKIP ); echo $before_widget; $title = empty( $instance[ 'title' ] ) ? __( 'Recent Posts', 'adv-recent-posts' ) : apply_filters( 'widget_title', $instance[ 'title' ] ); $link = empty( $instance[ 'link' ]) ? '' : $instance[ 'link' ]; $parameters = array( 'title' => $title, 'link' => $instance[ 'link' ], 'hideposttitle' => $instance[ 'hideposttitle' ], 'separator' => $instance[ 'separator' ], 'afterexcerpt' => $instance[ 'afterexcerpt' ], 'afterexcerptlink' => $instance[ 'afterexcerptlink' ], 'show_type' => $instance[ 'show_type' ], 'shownum' => (int) $instance[ 'shownum' ], 'postoffset' => (int) $instance[ 'postoffset' ], 'reverseorder' => (int) $instance[ 'reverseorder' ], 'excerpt' => (int) $instance[ 'excerpt' ], 'excerptlengthwords' => (int) $instance[ 'excerptlengthwords' ], 'actcat' => (bool) $instance[ 'actcat' ], 'cats' => esc_attr( $instance[ 'cats' ] ), 'cusfield' => esc_attr( $instance[ 'cusfield' ] ), 'w' => (int) $instance[ 'width' ], 'h' => (int) $instance[ 'height' ], 'firstimage' => (bool) $instance[ 'firstimage' ], 'atimage' =>(bool) $instance[ 'atimage' ], 'defimage' => esc_url( $instance[ 'defimage' ] ), 'showauthor' => (bool) $instance[ 'showauthor' ], 'showtime' => (bool) $instance[ 'showtime' ], 'format' => esc_attr( $instance[ 'format' ] ), 'spot' => esc_attr( $instance[ 'spot' ] ), ); if ( !empty( $title ) && !empty( $link ) ) { echo $before_title . '' . $title . '' . $after_title; } else if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } //print recent posts yg_recentposts($parameters); echo $after_widget; } //end of widget() //Update widget options function update($new_instance, $old_instance) { $instance = $old_instance; //get old variables $instance['title'] = esc_attr($new_instance['title']); $instance['link'] = esc_attr($new_instance['link']); $instance['hideposttitle'] = $new_instance['hideposttitle'] ? 1 : 0; $instance['separator'] = $new_instance['separator']; $instance['afterexcerpt'] = $new_instance['afterexcerpt']; $instance['afterexcerptlink'] = $new_instance['afterexcerptlink'] ? 1 : 0; $instance['show_type'] = $new_instance['show_type']; $instance['shownum'] = isset($new_instance['show-num']) ? (int) abs($new_instance['show-num']) : (int) abs($new_instance['shownum']); // if ($instance['shownum'] > 20) $instance['shownum'] = 20; unset($instance['show-num']); $instance['postoffset'] = (int) abs($new_instance['postoffset']); $instance['reverseorder'] = $new_instance['reverseorder'] ? 1 : 0; $instance['excerpt'] = isset($new_instance['excerpt-length']) ? (int) abs($new_instance['excerpt-length']) : (int) abs($new_instance['excerpt']); unset($instance['excerpt-length']); $instance['excerptlengthwords'] = (int) abs($new_instance['excerptlengthwords']); $instance['cats'] = esc_attr($new_instance['cats']); $instance['actcat'] = $new_instance['actcat'] ? 1 : 0; $instance['cusfield'] = isset($new_instance['cus-field']) ? esc_attr($new_instance['cus-field']) : esc_attr($new_instance['cusfield']); unset($instance['cus-field']); $instance['width'] = esc_attr($new_instance['width']); $instance['height'] = esc_attr($new_instance['height']); $instance['firstimage'] = $new_instance['first-image'] ? 1 : 0; $instance['atimage'] = $new_instance['atimage'] ? 1 : 0; $instance['defimage'] = esc_url($new_instance['def-image']); $instance['showauthor'] = $new_instance['showauthor'] ? 1 : 0; $instance['showtime'] = $new_instance['showtime'] ? 1 : 0; $instance['format'] = esc_attr($new_instance['format']); $instance['spot'] = esc_attr($new_instance['spot']); unset($instance['spot1']); unset($instance['spot2']); unset($instance['spot3']); //die($new_instance['spot']); return $instance; } //end of update() //Widget options form function form($instance) { if (isset($instance['spot1'])) $instance['spot'] = $instance['spot1']; if (isset($instance['show-num'])) $instance['shownum'] = $instance['show-num']; if (isset($instance['excerpt-length'])) $instance['excerpt'] = $instance['excerpt-length']; if (isset($instance['cus-field'])) $instance['cusfield'] = $instance['cus-field']; $instance = wp_parse_args( (array) $instance, yg_recentposts_defaults() ); $title = esc_attr($instance['title']); $link = esc_attr($instance['link']); $hideposttitle = $instance['hideposttitle']; $separator = $instance['separator']; $afterexcerpt = $instance['afterexcerpt']; $afterexcerptlink = $instance['afterexcerptlink']; $show_type = $instance['show_type']; $shownum = (int) $instance['shownum']; $postoffset = (int) $instance['postoffset']; $reverseorder = $instance['reverseorder']; $excerpt = (int) $instance['excerpt']; $excerptlengthwords = (int) $instance['excerptlengthwords']; $cats = esc_attr($instance['cats']); $actcat = (bool) $instance['actcat']; $cus_field = esc_attr($instance['cusfield']); $width = esc_attr($instance['width']); $height = esc_attr($instance['height']); $firstimage = (bool) $instance['firstimage']; $atimage = (bool) $instance['atimage']; $defimage = esc_url($instance['defimage']); $showauthor = (bool) $instance['showauthor']; $showtime = (bool) $instance['showtime']; $format = esc_attr($instance['format']); $spot = esc_attr($instance['spot']); ?>

/>

/>

/>


px
px

/>

/>

/>

/>


/>

__( 'Recent Posts', 'adv-recent-posts' ), 'link' => get_bloginfo( 'url' ) . '/blog/', 'hideposttitle' => 0, 'separator' => ': ', 'afterexcerpt' => '...', 'afterexcerptlink' => 0, 'show_type' => 'post', 'postoffset' => 0, 'limit' => 10, 'shownum' => 10, 'reverseorder' => 0, 'excerpt' => 0, 'excerptlengthwords' => 0, 'actcat' => 0, 'cats' => '', 'cusfield' => '', 'width' => '', 'height' => '', 'w' => '', 'h' => '', 'firstimage' => 0, 'showauthor' => 0, 'showtime' => 0, 'atimage' => 0, 'defimage' => '', 'format' => 'm/d/Y', 'spot' => 'spot1' ); return $defaults; } add_shortcode( 'amrp' , 'yg_recentposts_sc'); function yg_recentposts_sc( $atts ) { $defaults = yg_recentposts_defaults(); // $defaults['limit'] = $defaults['shownum']; // unset($shownum); $args = shortcode_atts($defaults, $atts); return yg_recentposts( $args, false ); } function yg_wordexcerpt() { } function yg_recentposts($args = '', $echo = true) { global $wpdb; $defaults = yg_recentposts_defaults(); //$defaults = array('separator' => ': ','show_type' => 'post', 'limit' => 10, 'excerpt' => 0, 'actcat' => 0, 'cats'=>'', 'cusfield' =>'', 'w' => 48, 'h' => 48, 'firstimage' => 0, 'showauthor' => 0, 'showtime' => 0, 'atimage' => 0, 'defimage' => '', 'format' => 'm/d/Y', 'spot' => 'spot1'); $args = wp_parse_args( $args, $defaults ); extract($args); $hideposttitle = (bool) $hideposttitle; $separator = $separator; $afterexcerpt = $afterexcerpt; $afterexcerptlink = (bool) $afterexcerptlink; $show_type = $show_type; $shownum = (int) abs($shownum); if(isset($limit) && $shownum == 10) $shownum = (int) $limit; $postoffset = (int) abs($postoffset); $reverseorder = (int) abs($reverseorder); $firstimage = (bool) $firstimage; $showauthor = (bool) $showauthor; $showtime = (bool) $showtime; $spot = esc_attr($spot); $atimage = (bool) $atimage; $defimage = esc_url($defimage); $format = esc_attr($format); $time = ''; $width = (int) $width; $height = (int) $height; $w = (int) $w; $h = (int) $h; if ($width > $w) { $width = $width; $height = $height; } else { $width = $w; $height = $h; } $excerptlength = (int) abs($excerpt); $excerptlengthwords = (int) abs($excerptlengthwords); $excerpt = ''; $cats = str_replace(" ", "", esc_attr($cats)); if (($shownum < 1 ) || ($shownum > 20)) $shownum = 10; /*$postlist = wp_cache_get('yg_recent_posts'); //Not yet if ( false === $postlist ) { */ if (($actcat) && (is_category())) { $cats = get_query_var('cat'); } if (($actcat) && (is_single())) { $cats = ''; foreach (get_the_category() as $catt) { $cats .= $catt->cat_ID.' '; } $cats = str_replace(" ", ",", trim($cats)); } if (!intval($cats)) $cats=''; $query = "cat=$cats&showposts=$shownum&post_type=$show_type&offset=$postoffset"; $posts = get_posts($query); //get posts if ($reverseorder) $posts = array_reverse($posts); $postlist = ''; $height = $height ? ' height: ' . $height .'px;' : ''; $width = $width ? ' width: ' . $width . 'px;' : ''; $hw = (!empty($height) || !empty($width)) ? 'style="'.$width.$height.'"' : ''; foreach ($posts as $post) { if ($showtime) { $time = ' '. date($format,strtotime($post->post_date)); } $post_title = stripslashes($post->post_title); if ($excerptlength) { $excerpt = $post->post_excerpt; $text = $post->post_content; $text = strip_shortcodes( $text ); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = 100; $words = explode(' ', $text, $excerpt_length + 1); if ( '' == $excerpt ) { if (count($words) > $excerpt_length) { array_pop($words); $text = implode(' ', $words); } $excerpt = $text; } $afterexcerpt_html = ''; if ($afterexcerptlink) $afterexcerpt_html = '' . $afterexcerpt . ''; if ($excerptlengthwords > 0 ) { $words = array_splice($words, 0, $excerptlengthwords); $excerpt = implode(' ', $words); }elseif(strlen($excerpt) > $excerptlength) { $excerpt = mb_substr($excerpt, 0, $excerptlength); } $excerpt = $separator . ($spot == 'spot3' ? ''.$time.' ' : '') . $excerpt . $afterexcerpt_html; } $image = ''; $img = ''; if ($cusfield) { $cusfield = esc_attr($cusfield); $img = get_post_meta($post->ID, $cusfield, true); } if (!$img && $firstimage) { $match_count = preg_match_all("//", $post->post_content, $match_array, PREG_PATTERN_ORDER); $img = count($match_array['1']) > 0 ? $match_array[1][0] : false; } if (!$img && $atimage) { $p = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1, 'order' => 'ASC', 'orderby' => 'menu_order ID', 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($p); if ($attachments) { $imgsrc = wp_get_attachment_image_src($attachments[0]->ID, 'thumbnail'); $img = $imgsrc[0]; } } if (!$img && $defimage) $img = $defimage; if ($img) $image = ''; $postlist .= '
  • '.($spot == 'spot1' ? ''.$time.' ' : ''); $postlist .= $image; if (!$hideposttitle) $postlist .= '' . $post_title .''; $author_data = get_userdata($post->post_author); $postlist .= ($showauthor ? ' by '.$author_data->display_name : '') . ($spot == 'spot2' ? ' '.$time.'' : '') . $excerpt . "
  • "; }// end foreach() /* wp_cache_set('yg_recent_posts', $postlist); }*/ if ($echo) echo ''; else return ''; } ?>