Tôi sẽ chia sẽ với anh chị code php để lấy các bài viết mới nhất với 2 tùy chọn là title và number
php code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php class last_posts extends WP_Widget { function __construct(){ $widget_ops = array('description' => 'Last posts'); $control_ops = array('width' => 300, 'height' => 300); parent::__construct(false,$name='Last posts',$widget_ops,$control_ops); } function form($instance){ global $wpdb; //Defaults $instance = wp_parse_args( (array) $instance, array('title'=>'','numbershow'=>'') ); $title = htmlspecialchars($instance['title']); $numbershow = htmlspecialchars($instance['numbershow']); ?> <p><label for="<?php echo $this->get_field_id('title'); ?>">Title </label> <input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" style="width:100%" value="<?php echo $title; ?>"> </p> <p><label for="<?php echo $this->get_field_id('numbershow'); ?>">Show number: </label> <input type="text" name="<?php echo $this->get_field_name('numbershow'); ?>" id="<?php echo $this->get_field_id('numbershow'); ?>" style="width:100%" value="<?php echo $numbershow; ?>"> </p> <?php } /*Saves the settings. */ function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = stripslashes($new_instance['title']); $instance['numbershow'] = stripslashes($new_instance['numbershow']); return $instance; } function widget($args, $instance) { extract($args); $title= empty($instance['title']) ? '' : $instance['title']; $numbershow= empty($instance['numbershow']) ? '' : $instance['numbershow']; echo $before_widget; if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; } ?> <?php $args = array( 'post_type' => 'post', 'posts_per_page' => $numbershow, 'post_status' => 'publish' ); $last_posts = new WP_Query( $args ); if ( $last_posts->have_posts() ) : while ( $last_posts->have_posts() ) : $last_posts->the_post(); ?> <!-- item post is here --> <?php endwhile; wp_reset_postdata(); endif; ?> <?php echo $after_widget; ?> <?php } }// end class register_widget('last_posts'); ?> |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress