Trong widget này mình lấy code mặc định của wordpress và có thêm phần chọn page mà bạn mình muốn hiển thị widget.
Vấn đề đặt ra là: Widget hiển thị bài viết mới nhất nhưng chỉ hiển thị ở trang Home và Blog còn những trang khác thì không hiển thị widget này. Vậy widget này sẽ giúp các bạn giải quyết được vấn đề trên
Download với Dropbox Download với Google Driver
Widget hiển thị bài viết mới nhất + tuỳ chỉnh page hiển thị
trước tiên bạn tạo 1 file với tên bất kỳ với nội dung bên dưới. sau đó include (hoặc require_once) vào file functions.php trong theme của bạn là được.
Với ví dụ này mình tạo file có tên og_recentpostcustoms.php với nội dung
<?php /** * Recent_Posts widget class * * Author Lê Văn Toản - https://levantoan.com */ class WP_Widget_Recent_Posts_Custom_SVL extends WP_Widget { public function __construct() { $widget_ops = array('classname' => 'widget_recent_entries_svl', 'description' => __( "Your site’s most recent Posts Custom view.") ); parent::__construct('recent-posts_svl', __('[SVL] Recent Posts Custom View'), $widget_ops); $this->alt_option_name = 'widget_recent_entries_svl'; 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') ); } public function widget($args, $instance) { ob_start(); $show_page = ( ! empty( $instance['show_page'] ) ) ? $instance['show_page'] : ''; if(is_page( $show_page) ): $cache = array(); if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'widget_recent_posts_svl', 'widget' ); } if ( ! is_array( $cache ) ) { $cache = array(); } if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' ); /** This filter is documented in wp-includes/default-widgets.php */ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; if ( ! $number ) $number = 5; $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) ); if ($r->have_posts()) : ?> <?php echo $args['before_widget']; ?> <?php if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } ?> <ul> <?php while ( $r->have_posts() ) : $r->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a> <?php if ( $show_date ) : ?> <span class="date"><?php echo get_the_date('d M, Y'); ?></span> <?php endif; ?> </li> <?php endwhile; ?> </ul> <?php echo $args['after_widget']; ?> <?php // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); endif; if ( ! $this->is_preview() ) { $cache[ $args['widget_id'] ] = ob_get_flush(); wp_cache_set( 'widget_recent_posts_svl', $cache, 'widget' ); } else { ob_end_flush(); } endif;//Ket thuc tuy chon hien thi } public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = (int) $new_instance['number']; $instance['show_page'] = $new_instance['show_page']; $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; $this->flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['widget_recent_entries_svl']) ) delete_option('widget_recent_entries_svl'); return $instance; } public function flush_widget_cache() { wp_cache_delete('widget_recent_posts_svl', 'widget'); } public function form( $instance ) { $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; $show_page = isset( $instance['show_page'] ) ? $instance['show_page'] : ''; $pages = get_pages(); ?> <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p> <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> <p> <label for="<?php echo $this->get_field_id('show_page'); ?>"><?php _e('ID Page to view:'); ?></label> <select style="width: 100%;" multiple="multiple" id="<?php echo $this->get_field_id('show_page'); ?>" name="<?php echo $this->get_field_name('show_page'); ?>[]"> <option value="0" style="padding:3px;"><?php _e( '— Select —' ) ?></option> <?php foreach ( $pages as $page ) { printf( '<option value="%s" %s style="padding:3px;">%s</option>', $page->ID, in_array( $page->ID, $show_page) ? 'selected="selected"' : '', $page->post_title ); } ?> </select> </p> <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" /> <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p> <?php } } function wp_widgets_init_recent_post_svl() { if ( !is_blog_installed() ) return; register_widget('WP_Widget_Recent_Posts_Custom_SVL'); do_action( 'widgets_init' ); } add_action('init', 'wp_widgets_init_recent_post_svl', 1);
Sau đó chèn đoạn code sau vào file functions.php của theme đang sử dụng:
require_once 'og_recentpostcustoms.php';
Hãy xem kết quả và tận hưởng thành quả nào 🙂
Download với Dropbox Download với Google Driver
Chúc các bạn thành công !
- Bình luận