在小部件中使用wp_category_checklist


8

我正在尝试在小部件中使用wp_category_checklist来显示复选框列表,这些复选框在保存时仍保持勾选状态。据我所知,目前保存它们时,我遇到了很大的麻烦(未选中复选框):-

这是我目前拥有的已编辑代码。

function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance['widget_categories'] = $new_instance['post_category'];
return $instance;
}


function form($instance) {
    $instance = wp_parse_args( (array) $instance, $default );
    $categories = get_categories();     

    $category_array = $instance['widget_categories'];

    if (!$category_array)
    {
        $category_array = array();
    }
    ?>
    <ul class="categorychecklist">
    <?php wp_category_checklist(0,0, $category_array,false, NULL , false);?>
    </ul>
    <?php
}

有任何想法吗?如果您还有其他需要,请告诉我。

谢谢 :)

Answers:


9

问题在于,要使update窗口小部件类的方法起作用,form应通过该方法设置名称输入,$this->get_get_field_name('name_of_the_field');wp_category_checklist不能使用参数来设置输入名称(复选框)。

但是,wp_category_checklist使用walker类来打印复选框并允许对其进行自定义。默认情况下,使用的类是Walker_Category_Checklist,并且打印复选框的方法是start_el

该方法没有允许编辑输入名称的过滤器,但是我们可以创建一个自定义的walker,该接受器可以使用参数来设置名称。如果walker扩展了Walker_Category_Checklist,我们只需要重写start_elmethod(主要是从原始副本复制)。

编码:

// This is required to be sure Walker_Category_Checklist class is available
require_once ABSPATH . 'wp-admin/includes/template.php';
/**
 * Custom walker to print category checkboxes for widget forms
 */
class Walker_Category_Checklist_Widget extends Walker_Category_Checklist {

    private $name;
    private $id;

    function __construct( $name = '', $id = '' ) {
        $this->name = $name;
        $this->id = $id;
    }

    function start_el( &$output, $cat, $depth = 0, $args = array(), $id = 0 ) {
        extract( $args );
        if ( empty( $taxonomy ) ) $taxonomy = 'category';
        $class = in_array( $cat->term_id, $popular_cats ) ? ' class="popular-category"' : '';
        $id = $this->id . '-' . $cat->term_id;
        $checked = checked( in_array( $cat->term_id, $selected_cats ), true, false );
        $output .= "\n<li id='{$taxonomy}-{$cat->term_id}'$class>" 
            . '<label class="selectit"><input value="' 
            . $cat->term_id . '" type="checkbox" name="' . $this->name 
            . '[]" id="in-'. $id . '"' . $checked 
            . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' 
            . esc_html( apply_filters( 'the_category', $cat->name ) ) 
            . '</label>';
      }
}

现在,可能在同一文件中,我们可以编写小部件类:

/**
 * An example of widget using wp_category_checklist on form
 */
class TestCategoryWidget extends WP_Widget {

    function __construct(){
        parent::__construct( false, 'TestWidget');
    }

    function widget( $args, $instance ) { 
        // Displays the widget on frontend 
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['widget_categories'] = $new_instance['widget_categories'];
        return $instance;
    }

    function form( $instance ) {
        $defaults = array( 'widget_categories' => array() );
        $instance = wp_parse_args( (array) $instance, $defaults );    
        // Instantiate the walker passing name and id as arguments to constructor
        $walker = new Walker_Category_Checklist_Widget(
            $this->get_field_name( 'widget_categories' ), 
            $this->get_field_id( 'widget_categories' )
        );
        echo '<ul class="categorychecklist">';
        wp_category_checklist( 0, 0, $instance['widget_categories'], FALSE, $walker, FALSE );
        echo '</ul>';
    }

}

最后,注册小部件:

function TestCategoryWidgetInit() {
    register_widget( 'TestCategoryWidget' );
}

add_action( 'widgets_init', 'TestCategoryWidgetInit' );
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.