wordpress自定义侧边栏小工具widget

wordpress提供了主题小工具支持(后台——外观——小工具),可以实现自由拖动功能模板到侧边栏,实现相关的调用显示。wordpress默认提供了若干个小工具,但大部分不能满足博主的建站需求,因此对小工具自定义扩展非常有必要。以下通过转载添加分类文章列表小工具的教程详细了解如何自定义侧边栏小工具。

wordpress自定义侧边栏小工具widget

概念性的东西:wordpress提供了一个WP_Widget类,我们只需要扩展WP_Widget类,就可以制作自己的小工具(widget),此类文件位于 wp-includeswidgets.php

下面老季给出一个完整的调用分类文章模块的widget工具完整代码:

<?php /** * 功能:调用某分类下的文章列表 * 调用:在主题functions.php文件里引入本文件 **/ /**   * Add function to widgets_init that'll load our widget.   * @since 0.1   */ add_action('widgets_init', 'load_relation_widgets'); /**   * Register our widget.   * 'Example_Widget' is the widget class used below.   *   * @since 0.1   */ function load_relation_widgets() {     register_widget('jtag_post_widget'); }   class jtag_post_widget extends WP_Widget {     public function jtag_post_widget()      {          $widget_ops  = array(              'classname'                   => 'widget_custom_html',              'description'                 => __( '关联标签分类文章调用.' ),              'customize_selective_refresh' => true,              'show_instance_in_rest'       => true,          );          $this->__construct('jtag_post_widget', $name = '关联标签分类文章调用', $widget_ops);      }      //小工具的选项设置表单     public function form($instance)     {         $instance = wp_parse_args((array)$instance, array('title' => '分类文章', 'showPosts' => 10, 'cat' => 0)); //默认值         $title = htmlspecialchars($instance['title']);         $showPosts = htmlspecialchars($instance['showPosts']);         $cat = htmlspecialchars($instance['cat']);         echo '<p style="text-align:left;"><label for="' . $this->get_field_name('title') . '">标题:<input style="width:200px;" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></label></p>';         echo '<p style="text-align:left;"><label for="' . $this->get_field_name('showPosts') . '">文章数量:<input style="width:200px;" id="' . $this->get_field_id('showPosts') . '" name="' . $this->get_field_name('showPosts') . '" type="text" value="' . $showPosts . '" /></label></p>';         echo '<p style="text-align:left;"><label for="' . $this->get_field_name('cat') . '">分类ID(多个分类ID,可用逗号分割):<input style="width:200px" id="' . $this->get_field_id('cat') . '" name="' . $this->get_field_name('cat') . '" type="text" value="' . $cat . '" /></label></p>';     }      public function update($new_instance, $old_instance)     {         $instance = $old_instance;         $instance['title'] = strip_tags(stripslashes($new_instance['title']));         $instance['showPosts'] = strip_tags(stripslashes($new_instance['showPosts']));         $instance['cat'] = strip_tags(stripslashes($new_instance['cat']));         return $instance;     }      public function the_rand_posts($args = '')     {         $default = array('showPosts' => 10, 'cat' => '1');         $r = wp_parse_args($args, $default);         extract($r);         $args = array(             'cat'   =>  $cat,             'showposts' =>  $showPosts,             'orderby'   =>  'desc',         );         $the_query = new WP_Query($args);         if ($the_query->have_posts()) {             echo '<ul>';             while ($the_query->have_posts()) {                 $the_query->the_post();                 echo '<li><a href="'%20.%20get_permalink()%20.%20'">' . get_the_title() . '</a></li>';             }             echo '</ul>';         }         // Restore original Post Data         wp_reset_postdata();     }      //小工具在前台显示效果     public function widget($args, $instance)     {         extract($args);         $title = apply_filters('widget_title', empty($instance['title']) ? __('分类文章<i>Title</i>') : $instance['title']);         $showPosts = empty($instance['showPosts']) ? 10 : $instance['showPosts'];         $cat = empty($instance['cat']) ? 0 : $instance['cat'];         echo $before_widget;         if ($title) echo $before_title . $title . $after_title;         $this->the_rand_posts("showPosts=$showPosts&cat='$cat'");         echo $after_widget;     } } 

腾讯云限时秒杀【点击购买】

搬瓦工,CN2高速线路,1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

阿里云香港、新加坡VPS/1核/1G/25G SSD/1T流量/30M带宽/年付¥288【点击购买】

wordpress自定义侧边栏小工具widget

`微信`扫码 加好友

链接到文章: https://gkxyz.com/wordpresszidingyicebianlanxiaogongjuwidget.html

推荐站点

  • RAKsmart

    主机商介绍:RAKsmart是知名的美国服务器租用商,包括美国站群服务器、美国cn2服务器、香港服务器等热门产 […]

  • Vultr

    Vultr Global Cloud Hosting – Brilliantly Fast SSD […]

  • 芒果TV-天生青春

    芒果TV-大家都在看的在线视频网站-热门综艺最新电影电视剧在线观看

  • 新闻中心首页_新浪网

    新浪网新闻中心是新浪网最重要的频道之一,24小时滚动报道国内、国际及社会新闻。每日编发新闻数以万计。

  • 阿里云-为了无法计算的价值

    阿里云——阿里巴巴集团旗下公司,是全球领先的云计算及人工智能科技公司之一。提供免费试用、云服务器、云数据库、云 […]

  • DigitalOcean – The developer cloud

    Helping millions of developers easily build, test, mana […]

评论已关闭