【问题标题】:how to add text widgets to wordpress sidebar using function?如何使用功能将文本小部件添加到 wordpress 侧边栏?
【发布时间】:2014-12-31 16:55:33
【问题描述】:

我想在激活主题时将文本小部件添加到我的站点侧边栏中。如何使用 wordpress 功能或其他方法做到这一点?我正在使用wordpress 4.1。 帮我。谢谢。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我不知道你要创建什么样的小部件,所以我给你一个简单的例子让你理解。

    在functions.php中编写代码或创建一个外部文件并将其包含在functions.php中

    首先注册小部件:

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

    第二次创建一个widget body:

    class Your_Simple_Same_Widget extends WP_Widget {  }
    

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

    类中有四个部分的widget

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

    Ok 班级第三 编写如下代码:

    //========= 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
        }
    
    }
    ?>
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-09
      • 2014-01-29
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多