* */ // Creating the simple_post widget class simple_post_widget extends WP_Widget { function __construct() { parent::__construct( // Base ID of your widget 'simple_post_widget', // Widget name will appear in UI __('Simple Post Widget', 'simple_post_widget_domain'), // Widget description array('description' => __('Simple Post widget ', 'simple_post_widget_domain'),) ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * Creating widget front-end * This is where the action happens * */ public function widget($args, $instance) { $title = apply_filters('widget_title', $instance['title']); $numberOfListings = apply_filters('widget_numberOfListings', $instance['numberOfListings']); $post_type = esc_attr($instance['post_type']); $show_date = esc_attr($instance['show_date']); $show_description = esc_attr($instance['show_description']); $show_thumb = esc_attr($instance['show_thumb']); $col = $show_thumb == 'on' ? 9 : 12; // $maxlines = esc_attr($instance['maxlines']); // before and after widget arguments are defined by themes echo $args['before_widget']; if (!empty($title)) echo $args['before_title'] . $title . $args['after_title']; if (empty($orderby)) $pfunc = 'post_date desc'; $gp_args = array( 'posts_per_page' => $numberOfListings, 'post_type' => $post_type, 'orderby' => 'post_date', 'order' => 'desc', 'post_status' => 'publish', ); // $loop = new WP_Query( $gp_args ); $posts = get_posts($gp_args); //var_dump($posts); foreach ($posts as $post): echo '
'; if($show_thumb){ if(has_post_thumbnail($post->ID)){ $col = 8; echo '
' . get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'img-responsive')) . '
'; }else{ $col = 12; } } echo "'; echo $post->post_title; if($show_date == 'on') echo '
' . date('Y-m-d', strtotime($post->post_date)) . '
'; if($show_description == 'on') { if (strlen($post->post_content) > 50) { echo substr($post->post_content, 0, 47) . '...'; } else substr($post->post_content, 0, 50); } echo '
'; echo '
'; endforeach; } // Widget Backend public function form($instance) { if (isset($instance['title'])) { $title = $instance['title']; } else { $title = __('New title', 'simple_post_widget_domain'); } if (isset($instance['numberOfListings'])) { $numberOfListings = $instance['numberOfListings']; } else { $numberOfListings = __('5', 'simple_post_widget_domain'); } $post_type = isset($instance['post_type']) ? esc_attr($instance['post_type']) : ''; $show_date = isset($instance['show_date']) ? esc_attr($instance['show_date']) : 'on'; $show_description = isset($instance['show_description']) ? esc_attr($instance['show_description']) : ''; $show_thumb = isset($instance['show_thumb']) ? esc_attr($instance['show_thumb']) : ''; // Widget admin form ?>

/>

/>

/>