2014-12-31 31 views

回答

0

我不知道你想創建什麼樣的小部件,所以我給你一個簡單的例子,所以你可以理解。在functions.php的

寫的打擊代碼或創建一個外部文件,並將其包含到的functions.php

首先註冊了Widget:

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

二創建一個widget體:

class Your_Simple_Same_Widget extends WP_Widget { } 

注意:register_widget('Your_Simple_Same_Widget');名稱和class Your_Simple_Same_Widget和小部件設置名稱必須相同

,我們在類四個部分插件的

  1. 窗口小部件設置
  2. 如何在前端顯示
  3. 更新的widget值
  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 
    } 

} 
?> 

我希望這可以幫助你。

相關問題