'pft-widget', 'description' => esc_html__( 'Display posts from selected tag', 'PFT') ); parent::__construct('PFTWidget', esc_html__( 'MT: Posts From Tag', 'PFT' ), $widget_ops, $control_ops); $this->alt_option_name = 'widget_pft'; } function pft_load_frontend_scripts() { wp_enqueue_style( 'pft-style', rtrim( plugin_dir_url( __FILE__ ), '/' ) . '/assets/pft-style.css' ); wp_enqueue_script( 'pft-custom', rtrim( plugin_dir_url( __FILE__ ), '/' ) . '/assets/pft-custom.js', array( 'jquery' ), '5.0.1' ); } /** * Displays the Widget * */ function widget($args, $instance){ $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); $layout = ! empty( $instance['layout'] ) ? $instance['layout'] : ''; $post_tag = ! empty( $instance['post_tag'] ) ? $instance['post_tag'] : 0; $post_order_by = ! empty( $instance['post_order_by'] ) ? $instance['post_order_by'] : ''; $post_order = ! empty( $instance['post_order'] ) ? $instance['post_order'] : ''; $post_num = ! empty( $instance['post_num'] ) ? $instance['post_num'] : 5; $post_exclude = ! empty( $instance['post_exclude'] ) ? $instance['post_exclude'] : ''; $post_length = !empty( $instance['post_length'] ) ? $instance['post_length'] : ''; $readmore_text = ! empty( $instance['readmore_text'] ) ? $instance['readmore_text'] : ''; // $show_posttitle = ! empty( $instance['show_posttitle'] ) ? $instance['show_posttitle'] : true; $date = ! empty( $instance['date'] ) ? $instance['date'] : false; $thumbnail = ! empty( $instance['thumbnail'] ) ? $instance['thumbnail'] : false; $post_thumbs = ! empty( $instance['post_thumbs'] ) ? $instance['post_thumbs'] : ''; echo $args['before_widget']; ?>
'', 'layout' => 'layout-one', 'post_tag' => '', 'post_order_by' => 'date', 'post_order' => 'DESC', 'post_num' => 5, 'post_exclude' => '', 'post_length' => 10, 'readmore_text' => esc_html__( 'Read More', 'PFT' ), // 'show_posttitle' => true, 'date' => true, 'thumbnail' => true, 'post_thumbs' => 'thumbnail', ) ); ?>

id="get_field_id( 'layout-one' ); ?>" name="get_field_name('layout'); ?>" value="layout-one" /> id="get_field_id( 'layout-two' ); ?>" name="get_field_name('layout'); ?>" value="layout-two" />

'name', 'hide_empty' => 1, 'class' => 'widefat', 'taxonomy' => 'post_tag', 'name' => $this->get_field_name( 'post_tag' ), 'id' => $this->get_field_id( 'post_tag' ), 'selected' => absint( $instance['post_tag'] ), 'show_option_all' => esc_html__( 'All Categories','PFT' ), ); wp_dropdown_categories( $cat_args ); ?>

dropdown_post_order_by( array( 'id' => $this->get_field_id( 'post_order_by' ), 'name' => $this->get_field_name( 'post_order_by' ), 'selected' => esc_attr( $instance['post_order_by'] ), ) ); ?>

dropdown_post_order( array( 'id' => $this->get_field_id( 'post_order' ), 'name' => $this->get_field_name( 'post_order' ), 'selected' => esc_attr( $instance['post_order'] ), ) ); ?>

id="get_field_id( 'date' ) ); ?>" name="get_field_name( 'date' ) ); ?>" />

id="get_field_id( 'thumbnail' ) ); ?>" name="get_field_name( 'thumbnail' ) ); ?>" />

dropdown_post_thumbnail( array( 'id' => $this->get_field_id( 'post_thumbs' ), 'name' => $this->get_field_name( 'post_thumbs' ), 'selected' => esc_attr( $instance['post_thumbs'] ), ) ); ?>

'', 'class' => 'widefat', 'name' => '', 'selected' => 0, ); $r = wp_parse_args( $args, $defaults ); $output = ''; $choices = array( 'author' => esc_html__('Author', 'PFT'), 'title' => esc_html__('Post Title', 'PFT'), 'ID' => esc_html__('Post ID', 'PFT'), 'date' => esc_html__('Date', 'PFT'), 'menu_order' => esc_html__('Menu Order', 'PFT'), 'comment_count' => esc_html__('Number of Comments', 'PFT'), 'rand' => esc_html__('Random', 'PFT') ); if ( ! empty( $choices ) ) { $output = "\n"; } echo $output; } function dropdown_post_order( $args ) { $defaults = array( 'id' => '', 'class' => 'widefat', 'name' => '', 'selected' => 0, ); $r = wp_parse_args( $args, $defaults ); $output = ''; $choices = array( 'ASC' => esc_html__('Ascending', 'PFT'), 'DESC' => esc_html__('Descending', 'PFT') ); if ( ! empty( $choices ) ) { $output = "\n"; } echo $output; } function dropdown_post_thumbnail( $args ) { $defaults = array( 'id' => '', 'class' => 'widefat', 'name' => '', 'selected' => 0, ); $r = wp_parse_args( $args, $defaults ); $output = ''; global $_wp_additional_image_sizes; $get_intermediate_image_sizes = get_intermediate_image_sizes(); $choices = array( 'thumbnail' => esc_html__('Thumbnail', 'PFT'), 'medium' => esc_html__('Medium', 'PFT'), 'large' => esc_html__('Large', 'PFT'), 'full' => esc_html__('Full (Original)', 'PFT') ); $show_dimension = true; if ( true === $show_dimension ) { foreach ( array( 'thumbnail', 'medium', 'large' ) as $key => $_size ) { $choices[ $_size ] = $choices[ $_size ] . ' (' . get_option( $_size . '_size_w' ) . 'x' . get_option( $_size . '_size_h' ) . ')'; } } if ( ! empty( $_wp_additional_image_sizes ) && is_array( $_wp_additional_image_sizes ) ) { foreach ( $_wp_additional_image_sizes as $key => $size ) { $choices[ $key ] = $key; if ( true === $show_dimension ) { $choices[ $key ] .= ' (' . $size['width'] . 'x' . $size['height'] . ')'; } } } if ( ! empty( $allowed ) ) { foreach ( $choices as $key => $value ) { if ( ! in_array( $key, $allowed ) ) { unset( $choices[ $key ] ); } } } if ( ! empty( $choices ) ) { $output = "\n"; } echo $output; } }// END class /** * Register widget. * * Calls 'widgets_init' action after widget has been registered. */ function pftwidget_init() { register_widget('PFTWidget'); } add_action('widgets_init', 'pftwidget_init'); // Shortcode added function pft_init_shortcode( $atts ) { extract( shortcode_atts( array( 'layout' => 'layout-one', 'cat' => '', 'order_by' => 'date', 'order' => 'DESC', 'post_number' => 5, 'exclude' => '', 'length' => 10, 'readmore' => esc_html__('Read More', 'PFT'), 'show_date' => true, 'show_image' => true, 'image_size' => 'thumbnail', ), $atts )); ob_start(); pft_call_shortcode_details( $layout, $cat, $order_by, $order, $post_number, $exclude, $length, $readmore, $show_date, $show_image, $image_size ); return ob_get_clean(); } add_shortcode( 'pft', 'pft_init_shortcode' ); // Shortcode function details function pft_call_shortcode_details( $layout, $post_tag, $order_by, $order, $post_num, $exclude, $desc_length, $readmore_text, $show_date, $show_thumbnail, $thumb_size ){ if( 'layout-one' == $layout ){ $layout_class = 'layout-one'; }else{ $layout_class = 'layout-two'; } $exclude_id = explode(',', $exclude); $p_args = array( 'orderby' => $order_by, 'order' => $order, 'no_found_rows' => true, 'post__not_in' => get_option( 'sticky_posts' ), 'ignore_sticky_posts' => true, 'post_status' => 'publish', 'posts_per_page' => absint( $post_num ), 'post__not_in' => $exclude_id ); if ( absint( $post_tag ) > 0 ) { $p_args['cat'] = absint( $post_tag ); } $p_query = new WP_Query( $p_args ); if($p_query->have_posts()){ ?>
have_posts()){ $p_query->the_post(); ?>

'name', 'hide_empty' => 1, 'taxonomy' => 'post_tag', ); $cat_count = 1; $cat_params[0] = array( 'text' => esc_html__( 'Select Tag', 'PFT' ), 'value' => 0 ); $post_tags = get_categories( $cat_args ); foreach ( $post_tags as $cat ) { $cat_params[$cat_count]['text'] = $cat->name; $cat_params[$cat_count]['value'] = $cat->term_id; $cat_count++; } //For image sizes $image_size[0] = array( 'text' => esc_html__( 'Select Image Size', 'PFT' ), 'value' => '' ); global $_wp_additional_image_sizes; $get_intermediate_image_sizes = get_intermediate_image_sizes(); $choices = array( 'thumbnail' => esc_html__('Thumbnail', 'PFT'), 'medium' => esc_html__('Medium', 'PFT'), 'large' => esc_html__('Large', 'PFT'), 'full' => esc_html__('Full (Original)', 'PFT') ); $show_dimension = true; if ( true === $show_dimension ) { foreach ( array( 'thumbnail', 'medium', 'large' ) as $key => $_size ) { $choices[ $_size ] = $choices[ $_size ] . ' (' . get_option( $_size . '_size_w' ) . 'x' . get_option( $_size . '_size_h' ) . ')'; } } if ( ! empty( $_wp_additional_image_sizes ) && is_array( $_wp_additional_image_sizes ) ) { foreach ( $_wp_additional_image_sizes as $key => $size ) { $choices[ $key ] = $key; if ( true === $show_dimension ) { $choices[ $key ] .= ' (' . $size['width'] . 'x' . $size['height'] . ')'; } } } if ( ! empty( $allowed ) ) { foreach ( $choices as $key => $value ) { if ( ! in_array( $key, $allowed ) ) { unset( $choices[ $key ] ); } } } if ( ! empty( $choices ) ) { $img_count = 1; foreach ( $choices as $key => $value ) { $image_size[$img_count]['text'] = $value; $image_size[$img_count]['value'] = $key; $img_count++; } } ?>