创建自定义类别小部件

创建自定义类别小部件

本文介绍了创建自定义类别小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码尝试采用 WP_Widget_Categories 类,并将其用作基于默认类别小部件的自定义类别小部件的基础.

但是我没有得到任何输出,并且该小部件没有显示在可用小部件"列表中.我做错了什么?

 'widget_categories', 'description' => __( "类别列表或下拉列表"));$this->WP_Widget('categories', __('Categories'), $widget_ops);}功能小部件($args,$instance){提取( $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";回声 $before_widget;如果( $title )回声 $before_title .$title .$after_title;$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);如果 ( $d ) {$cat_args['show_option_none'] = __('选择类别');wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));?><script type='text/javascript'>/* <![CDATA[ */var dropdown = document.getElementById("cat");函数 onCatChange() {if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;}}dropdown.onchange = onCatChange;/* ]]>*/<?php} 别的 {?><ul><?php$cat_args['title_li'] = '';wp_list_categories(apply_filters('widget_categories_args', $cat_args));?><?php}回声 $after_widget;}函数更新( $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;返回 $instance;}函数形式( $instance ) {//默认值$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 检查( $dropdown );?>/><label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('显示为下拉菜单');?></label><br/><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>"name="<?php echo $this->get_field_name('count'); ?>"<?php 检查( $count );?>/><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('显示帖子计数');?></label><br/><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>"name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php 检查( $hierarchical );?>/><label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e('显示层次');?></label></p><?php}}函数 my_categories_init(){register_sidebar_widget(__('My Categories Widget'), 'MY_Widget_Categories');}add_action("plugins_loaded", "my_categories_init");?>
解决方案

您需要使用 register_widget 而不是 register_sidebar_widget,因为该函数是针对旧样式的小部件(没有扩展 WP_Widget 或者只是函数).您应该从一个挂钩到 widgets_init 操作的函数中执行此操作.有关详细信息,请参阅 Wordpress 小部件 API 文档:http://codex.wordpress.org/Widgets_API>

以下插件适用于 WP 2.9.

/*插件名称:我的类别小部件版本:0.1*/类 My_Widget_Categories 扩展 WP_Widget {函数 My_Widget_Categories() {$widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "我的类别列表或下拉列表"));$this->WP_Widget('my_categories', __('My Categories'), $widget_ops);}功能小部件($args,$instance){提取( $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";回声 $before_widget;如果( $title )回声 $before_title .$title .$after_title;$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);如果 ( $d ) {$cat_args['show_option_none'] = __('选择类别');wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));?><script type='text/javascript'>/* <![CDATA[ */var dropdown = document.getElementById("cat");函数 onCatChange() {if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;}}dropdown.onchange = onCatChange;/* ]]>*/<?php} 别的 {?><ul><?php$cat_args['title_li'] = '';wp_list_categories(apply_filters('widget_categories_args', $cat_args));?><?php}回声 $after_widget;}函数更新( $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;返回 $instance;}函数形式( $instance ) {//默认值$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 检查( $dropdown );?>/><label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('显示为下拉菜单');?></label><br/><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>"name="<?php echo $this->get_field_name('count'); ?>"<?php 检查( $count );?>/><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('显示帖子计数');?></label><br/><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>"name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php 检查( $hierarchical );?>/><label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e('显示层次');?></label></p><?php}}add_action('widgets_init', create_function('', "register_widget('My_Widget_Categories');"));

The code below is an attempt to take the WP_Widget_Categories class and use it as the basis for a custom categories widget based on the default categories widget.

I'm getting no output however and the widget is not showing up in the "Available Widgets" listing. What am I doing wrong?

<?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");
?>
解决方案

You'll need to use register_widget rather than register_sidebar_widget, as that function is for the old style widgets (that didn't extend WP_Widget or were just functions). You should to do this from a function hooked into the widgets_init action. See the Wordpress widgets API documentation for more info: http://codex.wordpress.org/Widgets_API


The following plugin works for me in 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');"));

这篇关于创建自定义类别小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:22