如何使用功能将文本小部件添加到WordPress侧边栏


how to add text widgets to wordpress sidebar using function?

我想

在激活主题时将文本小部件添加到我的网站侧边栏中。如何使用wordpress功能或其他方法执行此操作?我正在使用wordpress 4.1。 帮帮我。谢谢。

我不知道

你想创建什么样的小部件,所以我给你一个简单的例子,以便你理解。

在函数中编写打击代码.php或创建外部文件并将其包含在函数中.php

首先注册小部件:

//========= Your Custom Widget
add_action( 'widgets_init', 'your_simple_widget' );
function your_simple_widget() {
    register_widget( 'Your_Simple_Same_Widget' );
}

第二个创建小部件主体:

class Your_Simple_Same_Widget extends WP_Widget {  }

注意:register_widget( 'Your_Simple_Same_Widget' );名称和class Your_Simple_Same_Widget以及小部件设置名称必须相同

类中有四个部分的小部件

  1. 小部件设置
  2. 如何在前端显示
  3. 更新微件值
  4. 管理面板中的小部件表单或字段

好的 班级第三名 像这样写这段代码:

//========= Your Custom Widget Body
class Your_Simple_Same_Widget extends WP_Widget {
    //=======> Widget setup
    function Your_Simple_Same_Widget() {
        /* Widget settings. */
    $widget_ops = array( 'classname' => 'your class', 'description' => __('Your Class Description widget', 'your class') );
        /* Widget control settings. */
    $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'your_widget' );
    /* Create the widget. */
    $this->WP_Widget( 'your_widget', __('Widget Heading', 'your class'), $widget_ops, $control_ops );
    }
    //=======> How to display the widget on the screen.
    function widget($args, $widgetData) {
        extract($args);
        //=======> Our variables from the widget settings.
        $your_widget_title = $widgetData['txtYourTitle'];
        $your_widget_description = $widgetData['txtYourDescription'];
        //=======> widget body
        echo $before_widget;
        echo '<div class="">';
                if($your_widget_title){
                    echo '<h2>'.$your_widget_title .'</h2>';
                }
        if($your_widget_description){
                    echo '<p>'.$your_widget_description .'</p>';
        }
        echo "</div>";
        echo $after_widget;
    }
    //=======>Update the widget 
    function update($new_widgetData, $old_widgetData) {
        $widgetData = $old_widgetData;
        //Strip tags from title and name to remove HTML 
        $widgetData['txtYourTitle'] = $new_widgetData['txtYourTitle'];
        $widgetData['txtYourDescription'] = $new_widgetData['txtYourDescription'];
        return $widgetData;
    }
    //=======>widget Display
    function form($widgetData) {
        //Set up some default widget settings.
        $widgetData = wp_parse_args((array) $widgetData);
?>
        <p>
            <label for="<?php echo $this->get_field_id('txtYourTitle'); ?>">Widget Title:</label>
            <input id="<?php echo $this->get_field_id('txtYourTitle'); ?>" name="<?php echo $this->get_field_name('txtYourTitle'); ?>" value="<?php echo $widgetData['txtYourTitle']; ?>" style="width:275px;" />
        </p>
    <p>
            <label for="<?php echo $this->get_field_id('txtYourDescription'); ?>">Widget Title:</label>
            <input id="<?php echo $this->get_field_id('txtYourDescription'); ?>" name="<?php echo $this->get_field_name('txtYourDescription'); ?>" value="<?php echo $widgetData['txtYourDescription']; ?>" style="width:275px;" />
        </p>
<?php
    }
}
?>

我希望这对你有所帮助。