首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何使用函数将文本小工具添加到wordpress侧边栏?

如何使用函数将文本小工具添加到wordpress侧边栏?
EN

Stack Overflow用户
提问于 2015-01-01 00:55:34
回答 1查看 550关注 0票数 1

我想添加文本小工具到我的网站侧边栏,当我激活主题。如何使用wordpress函数或其他方法来完成此操作?我使用的是wordpress 4.1。帮帮我。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-01-02 14:21:36

我不知道你想创建什么样的小部件,所以我给你一个简单的例子,这样你就能理解了。

用functions.php编写blow代码,或者创建一个外部文件并将其包含到functions.php中

首先注册Widget:

代码语言:javascript
代码运行次数:0
运行
复制
//========= Your Custom Widget
add_action( 'widgets_init', 'your_simple_widget' );
function your_simple_widget() {
    register_widget( 'Your_Simple_Same_Widget' );
}

第二,创建一个小部件主体:

代码语言:javascript
代码运行次数:0
运行
复制
class Your_Simple_Same_Widget extends WP_Widget {  }

注意: register_widget( 'Your_Simple_Same_Widget' );名称与class Your_Simple_Same_Widget和微件安装程序名称必须相同

类中的widget有四个部分

  1. 小部件安装程序
  2. How to display on front end
  3. 更新小部件管理面板中的表单或字段

好的,第三,在类中编写如下代码:

代码语言:javascript
代码运行次数:0
运行
复制
//========= 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
    }

}
?>

我希望这能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27723991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档