如何在不覆盖WordPress默认组件的情况下创建自定义近期文章小工具?
如何在不覆盖WordPress默认组件的前提下创建自定义近期文章小工具?
当然可以!完全不用动WordPress自带的默认近期文章小工具,你可以独立开发一款属于自己的自定义版本,让用户自由选择使用哪一个。下面是具体的实现步骤:
1. 创建自定义小工具类
首先,你需要创建一个继承自WP_Widget的新类,这是WordPress小工具开发的标准方式,不会和默认组件产生冲突。示例代码如下:
class Custom_Recent_Posts_Widget extends WP_Widget { // 初始化小工具基本信息 public function __construct() { parent::__construct( 'custom_recent_posts', // 小工具唯一ID __('自定义近期文章', 'text-domain'), // 后台显示的小工具名称 array( 'description' => __('可自定义展示样式的近期文章小工具', 'text-domain') ) // 描述 ); } // 后台设置表单 public function form( $instance ) { // 默认设置 $defaults = array( 'title' => __('近期文章', 'text-domain'), 'number' => 5, 'show_date' => true, 'show_thumbnail' => true ); $instance = wp_parse_args( (array) $instance, $defaults ); // 输出标题输入框 echo '<p>'; echo '<label for="' . $this->get_field_id('title') . '">' . __('标题:', 'text-domain') . '</label>'; echo '<input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . esc_attr($instance['title']) . '" />'; echo '</p>'; // 输出显示数量输入框 echo '<p>'; echo '<label for="' . $this->get_field_id('number') . '">' . __('显示文章数量:', 'text-domain') . '</label>'; echo '<input class="tiny-text" id="' . $this->get_field_id('number') . '" name="' . $this->get_field_name('number') . '" type="number" value="' . esc_attr($instance['number']) . '" min="1" />'; echo '</p>'; // 输出显示日期选项 echo '<p>'; echo '<input type="checkbox" id="' . $this->get_field_id('show_date') . '" name="' . $this->get_field_name('show_date') . '" ' . checked($instance['show_date'], true, false) . '/>'; echo '<label for="' . $this->get_field_id('show_date') . '">' . __('显示发布日期', 'text-domain') . '</label>'; echo '</p>'; // 输出显示缩略图选项 echo '<p>'; echo '<input type="checkbox" id="' . $this->get_field_id('show_thumbnail') . '" name="' . $this->get_field_name('show_thumbnail') . '" ' . checked($instance['show_thumbnail'], true, false) . '/>'; echo '<label for="' . $this->get_field_id('show_thumbnail') . '">' . __('显示文章缩略图', 'text-domain') . '</label>'; echo '</p>'; } // 保存后台设置 public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = sanitize_text_field($new_instance['title']); $instance['number'] = absint($new_instance['number']); $instance['show_date'] = isset($new_instance['show_date']) ? true : false; $instance['show_thumbnail'] = isset($new_instance['show_thumbnail']) ? true : false; return $instance; } // 前台渲染小工具内容 public function widget( $args, $instance ) { extract($args); // 获取设置参数 $title = apply_filters('widget_title', $instance['title']); $number = $instance['number']; $show_date = $instance['show_date']; $show_thumbnail = $instance['show_thumbnail']; // 输出小工具包装器 echo $before_widget; if ( $title ) { echo $before_title . $title . $after_title; } // 查询近期文章 $recent_posts = new WP_Query(array( 'posts_per_page' => $number, 'post_status' => 'publish', 'ignore_sticky_posts' => true )); // 输出文章列表 if ( $recent_posts->have_posts() ) { echo '<ul class="custom-recent-posts">'; while ( $recent_posts->have_posts() ) { $recent_posts->the_post(); echo '<li>'; if ( $show_thumbnail && has_post_thumbnail() ) { echo '<a href="' . get_permalink() . '" class="recent-post-thumbnail">'; the_post_thumbnail('thumbnail'); echo '</a>'; } echo '<a href="' . get_permalink() . '" class="recent-post-title">' . get_the_title() . '</a>'; if ( $show_date ) { echo '<span class="recent-post-date">' . get_the_date() . '</span>'; } echo '</li>'; } echo '</ul>'; wp_reset_postdata(); } else { echo '<p>' . __('暂无文章', 'text-domain') . '</p>'; } echo $after_widget; } }
2. 注册自定义小工具
把上面的类添加到你的主题functions.php文件或者自定义插件中后,还需要通过widgets_init钩子注册这个小工具:
function register_custom_recent_posts_widget() { register_widget('Custom_Recent_Posts_Widget'); } add_action('widgets_init', 'register_custom_recent_posts_widget');
3. 自定义样式(可选)
为了让你的小工具样式更美观,你可以添加一些CSS代码到主题的style.css文件中:
.custom-recent-posts { list-style: none; padding: 0; margin: 0; } .custom-recent-posts li { margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid #eee; } .recent-post-thumbnail { float: left; margin-right: 10px; } .recent-post-title { display: block; font-weight: bold; text-decoration: none; } .recent-post-date { display: block; font-size: 0.8em; color: #666; }
这样做之后,用户在后台的小工具区域就能看到你新建的“自定义近期文章”小工具了,他们可以自由选择添加默认的还是你的自定义版本,完全不会互相干扰。你还可以根据需求扩展更多选项,比如添加分类筛选、是否显示摘要、排序方式等,让小工具的灵活性更高。
内容的提问来源于stack exchange,提问作者Alejandro Torres
相关产品推荐
相关产品推荐

