WordPress 侧边栏小工具
像素鱼丸
11-22
116
0

小工具介绍

WordPress 侧边栏小工具通常用在控制侧边栏显示的区块。

自 WordPress 5.8 开始,旧版小工具被古腾堡的块小工具所取代。想要从古腾堡小工具恢复到经典小工具,需要如下代码:

add_filter('gutenberg_use_widgets_block_editor', '__return_false');
add_filter('use_widgets_block_editor', '__return_false');

注册一个侧边栏 register_sidebars

function rt_widgets_init() {
    $args = array(
        'name' => __( '小工具标题', 'rt' ),
        'id' => 'sidebar-1',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h4>',
        'after_title' => '</h4>',
    );
    register_sidebar($args);
}
add_action('init', 'rt_widgets_init');

注册一个小工具 register_widget

register_widget注册的部件类中最终也是调用wp_register_sidebar_widget,注册的小部件。

class TestWidget extends WP_Widget{
    function __construct(){
        $this->WP_Widget( 'my_test', '小工具标题', array( 'description' => '小工具描述' ) );
    }
 
    function widget( $args, $instance ){
        extract( $args, EXTR_SKIP );
        echo $before_widget;
        echo "<div style='width:100%;height:200px;outline:1px solid #333;'><div>小工具内容</div></div>";
        echo $after_widget;
    }
}

function rt_add_widget(){
    register_widget( 'TestWidget' );
}

add_action( 'widgets_init', 'rt_add_widget' );

输出侧边栏 dynamic_sidebar

输出注册好的侧边栏,通过索引指定输出某一个侧边栏

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
<?php endif; ?>

输出小工具 the_widget

the_widget( string $widget,  array $instance = array(),  array $args = array() )

$widget 小工具的PHP名字

WP_Widget_Archives — Archives
WP_Widget_Calendar — Calendar
WP_Widget_Categories — Categories
WP_Widget_Links — Links
WP_Widget_Meta — Meta
WP_Widget_Pages — Pages
WP_Widget_Recent_Comments — Recent Comments
WP_Widget_Recent_Posts — Recent Posts
WP_Widget_RSS — RSS
WP_Widget_Search — Search (a search from)
WP_Widget_Tag_Cloud — Tag Cloud
WP_Widget_Text — Text
WP_Nav_Menu_Widget

$instance

表示每个widget的设置,例如Archives是用dropdown菜单显示还是列表显示

$args

widget的sidebar参数,包括before_widget、after_widget、before_title和after_title

给小工具添加设置选项

class Widget_Name extends WP_Widget {
    function __construct() {
        $widget_options = array(
            'classname' => 'widget_name',
            'description' => 'Widget short description.'
        );
        parent::__construct(
            'widget_name', // 小工具ID
            'Widget Name', // 小工具名称
            $widget_options // 小工具选项
        );
    }
    // 小工具前端显示函数
    function widget($args, $instance) {}

    // 小工具设置项显示函数
    function form($instance) {}

    // 小工具设置项保存函数
    function update($new_instance, $old_instance) {}
} 

小工具设置项显示函数

function form($instance) {
    $title = !empty($instance['title']) ? $instance['title'] : '';
    $text = !empty($instance['text']) ? $instance['text'] : '';
    ?>
    <p>
        <label for="<?php echo $this->get_field_id('title'); ?>">标题:</label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title); ?>">
    </p>
    <p>
        <label for="<?php echo $this->get_field_id('text'); ?>">描述:</label>
        <textarea class="widefat" rows="2" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo esc_html($text); ?></textarea>
    </p>
    <?php
}

小工具设置项保存函数

function update($new_instance, $old_instance) {
    $instance = array();
    $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
    $instance['text'] = (!empty($new_instance['text'])) ? strip_tags($new_instance['text']) : '';
    return $instance;
} 
收藏
打赏
WordPress 调用自定义头像
上一篇
实现自定义 Ajax 请求
下一篇

发表评论

注册不是必须的

像素鱼丸
150 文章
0 评论
4 喜欢
最新文章

宝塔 Nginx 拦截了 WordPress 返回 404 的状态

REST api 接口中,有段代码返回404,但是没有正常返回错误json: $wp_response = new \WP_REST_Response(array('error'=>'没有找到数据')); $wp_response->set_status(404); return $wp_response; 返回内容是: <html> <head><tit […]

Mirage 主题 v2.93.0 发布

更新内容: refactor 移除图片高宽比开关 refactor 移除全局的TOC生成开关 refactor 优化 header.php 中的seo模块和样式覆盖 feat 主题启用的时候,移除非当前主题注册的小工具 fix 修复分类小工具的bug feat 管理员打开后台,检查最当前设置首页布局的模块,如果缺少最新模块,就添加到隐藏模块列表中 fix 优化链接卡片样式 fix 优化热门文章样式 […]

如何使用 WordPress Setting API

使用 WordPress 的 Setting API 是在插件或主题中创建和管理设置页面的标准方式。它提供了一种结构化、安全的方式来保存和获取用户配置的选项。 ✅ 一、Setting API 简介 WordPress 的 Setting API 允许你: 创建设置页面(Settings Page) 注册设置字段(Settings Field) 验证和保存设置数据 使用表单提交来更新设置 ✅ 二、基 […]

详解 WordPress 的评论设置

好的,我们来详细梳理并总结 WordPress 中关于文章评论的两个核心控制层级:全局设置和单篇设置。理解这两者的关系(优先级)是管理网站评论的关键。 1. 全局设置 (Global Settings) —— 网站的“默认规则” 这是整个网站评论系统的总开关和默认行为准则。它决定了新发布的文章默认是什么样子的。 位置:WordPress 后台仪表盘 -> 设置 (Settings) -> […]
生成中...
扫描二维码
扫描二维码
用户登录