2010-04-22 112 views
2

下面的代碼嘗試獲取WP_Widget_Categories類,並將其用作基於默認類別小部件的自定義類別小部件的基礎。創建自定義類別小部件

但是我沒有收到任何輸出,並且該小部件沒有顯示在「可用小部件」列表中。我究竟做錯了什麼?

<?php 
/* 
Plugin Name: My Categories Widget 
Version: 1.0 
*/ 


class MY_Widget_Categories extends WP_Widget { 

    function MY_Widget_Categories() { 
     $widget_ops = array('classname' => 'widget_categories', 'description' => __("A list or dropdown of categories")); 
     $this->WP_Widget('categories', __('Categories'), $widget_ops); 
    } 

    function widget($args, $instance) { 
     extract($args); 

     $title = apply_filters('widget_title', empty($instance['title']) ? __('Categories') : $instance['title']); 
     $c = $instance['count'] ? '1' : '0'; 
     $h = $instance['hierarchical'] ? '1' : '0'; 
     $d = $instance['dropdown'] ? '1' : '0'; 

     echo $before_widget; 
     if ($title) 
      echo $before_title . $title . $after_title; 

     $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h); 

     if ($d) { 
      $cat_args['show_option_none'] = __('Select Category'); 
      wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args)); 
?> 

<script type='text/javascript'> 
/* <![CDATA[ */ 
    var dropdown = document.getElementById("cat"); 
    function onCatChange() { 
     if (dropdown.options[dropdown.selectedIndex].value > 0) { 
      location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value; 
     } 
    } 
    dropdown.onchange = onCatChange; 
/* ]]> */ 
</script> 

<?php 
     } else { 
?> 
     <ul> 
<?php 
     $cat_args['title_li'] = ''; 
     wp_list_categories(apply_filters('widget_categories_args', $cat_args)); 
?> 
     </ul> 
<?php 
     } 

     echo $after_widget; 
    } 

    function update($new_instance, $old_instance) { 
     $instance = $old_instance; 
     $instance['title'] = strip_tags($new_instance['title']); 
     $instance['count'] = $new_instance['count'] ? 1 : 0; 
     $instance['hierarchical'] = $new_instance['hierarchical'] ? 1 : 0; 
     $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0; 

     return $instance; 
    } 

    function form($instance) { 
     //Defaults 
     $instance = wp_parse_args((array) $instance, array('title' => '')); 
     $title = esc_attr($instance['title']); 
     $count = isset($instance['count']) ? (bool) $instance['count'] :false; 
     $hierarchical = isset($instance['hierarchical']) ? (bool) $instance['hierarchical'] : false; 
     $dropdown = isset($instance['dropdown']) ? (bool) $instance['dropdown'] : false; 
?> 
     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
     <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> 

     <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked($dropdown); ?> /> 
     <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Show as dropdown'); ?></label><br /> 

     <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked($count); ?> /> 
     <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label><br /> 

     <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked($hierarchical); ?> /> 
     <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e('Show hierarchy'); ?></label></p> 
<?php 
    } 

} 

function my_categories_init() 
{ 
    register_sidebar_widget(__('My Categories Widget'), 'MY_Widget_Categories'); 
} 

add_action("plugins_loaded", "my_categories_init"); 
?> 

回答

3

你需要使用register_widget而非register_sidebar_widget,作爲功能是舊式的小部件(即沒有延伸WP_Widget或只是功能)。您應該通過掛鉤到widgets_init操作的功能來完成此操作。請參閱WordPress的小工具API文檔的詳細信息:http://codex.wordpress.org/Widgets_API


以下插件對我的作品在WP 2.9。

/* 
Plugin Name: My Categories Widget 
Version: 0.1 
*/ 

class My_Widget_Categories extends WP_Widget { 

    function My_Widget_Categories() { 
     $widget_ops = array('classname' => 'widget_categories', 'description' => __("My list or dropdown of categories")); 
     $this->WP_Widget('my_categories', __('My Categories'), $widget_ops); 
    } 

    function widget($args, $instance) { 
     extract($args); 

     $title = apply_filters('widget_title', empty($instance['title']) ? __('Categories') : $instance['title']); 
     $c = $instance['count'] ? '1' : '0'; 
     $h = $instance['hierarchical'] ? '1' : '0'; 
     $d = $instance['dropdown'] ? '1' : '0'; 

     echo $before_widget; 
     if ($title) 
      echo $before_title . $title . $after_title; 

     $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h); 

     if ($d) { 
      $cat_args['show_option_none'] = __('Select Category'); 
      wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args)); 
?> 

<script type='text/javascript'> 
/* <![CDATA[ */ 
    var dropdown = document.getElementById("cat"); 
    function onCatChange() { 
     if (dropdown.options[dropdown.selectedIndex].value > 0) { 
      location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value; 
     } 
    } 
    dropdown.onchange = onCatChange; 
/* ]]> */ 
</script> 

<?php 
     } else { 
?> 
     <ul> 
<?php 
     $cat_args['title_li'] = ''; 
     wp_list_categories(apply_filters('widget_categories_args', $cat_args)); 
?> 
     </ul> 
<?php 
     } 

     echo $after_widget; 
    } 

    function update($new_instance, $old_instance) { 
     $instance = $old_instance; 
     $instance['title'] = strip_tags($new_instance['title']); 
     $instance['count'] = $new_instance['count'] ? 1 : 0; 
     $instance['hierarchical'] = $new_instance['hierarchical'] ? 1 : 0; 
     $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0; 

     return $instance; 
    } 

    function form($instance) { 
     //Defaults 
     $instance = wp_parse_args((array) $instance, array('title' => '')); 
     $title = esc_attr($instance['title']); 
     $count = isset($instance['count']) ? (bool) $instance['count'] :false; 
     $hierarchical = isset($instance['hierarchical']) ? (bool) $instance['hierarchical'] : false; 
     $dropdown = isset($instance['dropdown']) ? (bool) $instance['dropdown'] : false; 
?> 
     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
     <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> 

     <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked($dropdown); ?> /> 
     <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Show as dropdown'); ?></label><br /> 

     <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked($count); ?> /> 
     <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label><br /> 

     <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked($hierarchical); ?> /> 
     <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e('Show hierarchy'); ?></label></p> 
<?php 
    } 

} 

add_action('widgets_init', create_function('', "register_widget('My_Widget_Categories');")); 
+1

我想我應該在我的回答給了一個例子,你剛纔的問題,我們對此深感抱歉... – 2010-04-22 17:43:27

+1

我想我長的路:(更改爲register_widget回報:致命錯誤:調用一個成員函數註冊()在非線對象在C:\ xampplite \ htdocs \ wordpress \ wp-includes \ widgets.php在線431 (WP 2.9.2) – 2010-04-22 18:04:11

+1

完美Richard。謝謝。 exclude = 1參數,以便cat_id 1及其子項不顯示? – 2010-04-22 19:24:51

-1

我想你可以使用內置的wp函數來使用它。

<?php 
    $args = array(
        'name'    => $this->get_field_name('category'), 
     'show_option_none' => __('Select category'), 
     'show_count'  => 1, 
     'orderby'   => 'name', 
     'echo'    => 0, 
        'selected'   => $category, 
        'class'   => 'widefat' 
    ); 
      echo wp_dropdown_categories($args); 
     ?>