'related-posts', 'description' => __( 'This widget randomly display related post in content template' , 'black-white' ) ); $this->WP_Widget( 'bw_related_posts_widget' , __( 'BW Random Related Post' , 'black-white' ) , $widget_ops ); } function widget( $args, $instance ) { global $post; extract( $args , EXTR_SKIP ); $title = !empty( $instance[ 'title' ] )? $instance[ 'title' ] : ''; $type = !empty( $instance[ 'type' ] )? $instance[ 'type' ] : ''; $number = !empty( $instance[ 'number' ] )? (int)$instance[ 'number' ] : 5; $terms = wp_get_post_terms( $post -> ID , $type ); $terms_ = array(); if( empty( $terms ) ){ return; } foreach( $terms as $t ){ array_push( $terms_ , $t -> term_id ); } $type_terms = array( 'post_tag' => 'tag_in', 'category' => 'category_in' ); $args = array( $type_terms[ $type ] => $terms_ , 'post__not_in' => array( $post -> ID ), 'posts_per_page' => $number, 'orderby' => 'rand' ); $query = new WP_Query( $args ); if( empty( $query -> posts ) ){ return; } echo $before_widget; if( !empty( $title ) ) echo $before_title . $title . $after_title; echo '
'; echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance[ 'title' ] = esc_attr( $new_instance[ 'title' ] ); $instance[ 'type' ] = esc_attr( $new_instance[ 'type' ] ); $instance[ 'number' ] = (int)esc_attr( $new_instance[ 'number' ] ); return $instance; } function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'type' => 'post_tag', 'number' => 5, )); $title = $instance[ 'title' ]; $type = $instance[ 'type' ]; $number = $instance[ 'number' ]; ?> __( 'Categories' , 'black-white' ), 'post_tag' => __( 'Tags' , 'black-white' ) ); ?>'widget_bw_recent_post_format', 'description' => __( 'Recent post formatted as Aside, Image, Link, Quote, Status, and Video', 'black-white' ) ); $this->WP_Widget( 'widget_bw_recent_post_format', __( 'BW Recent Posts by Format', 'black-white' ), $widget_ops ); $this->alt_option_name = 'widget_bw_recent_post_format'; add_action( 'save_post', array(&$this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) ); add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) ); } function widget( $args, $instance ) { $cache = wp_cache_get( 'widget_bw_recent_post_format', 'widget' ); if ( !is_array( $cache ) ) $cache = array(); if ( ! isset( $args['widget_id'] ) ) $args['widget_id'] = null; if ( isset( $cache[$args['widget_id']] ) ) { echo $cache[$args['widget_id']]; return; } ob_start(); extract( $args, EXTR_SKIP ); $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Posts by Format', 'black-white' ) : $instance['title'], $instance, $this->id_base); if ( ! isset( $instance['number'] ) ) $instance['number'] = '10'; if ( ! $number = absint( $instance['number'] ) ) $number = 10; $bw_recent_args = array( 'order' => 'DESC', 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'post__not_in' => get_option( 'sticky_posts' ), 'tax_query' => array( array( 'taxonomy' => 'post_format', 'terms' => array( 'post-format-aside', 'post-format-image', 'post-format-link', 'post-format-quote', 'post-format-status', 'post-format-video' ), 'field' => 'slug', 'operator' => 'IN', ), ), ); $bw_recent = new WP_Query( $bw_recent_args ); if ( $bw_recent->have_posts() ) : echo $before_widget; echo $before_title; echo $title; echo $after_title; ?> flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset( $alloptions['widget_bw_recent_post_format'] ) ) delete_option( 'widget_bw_recent_post_format' ); return $instance; } function flush_widget_cache() { wp_cache_delete( 'widget_bw_recent_post_format', 'widget' ); } function form( $instance ) { $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : ''; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 10; ?>