在小部件选项中使用wp_Dropdown_Categories

时间:2012-11-26 作者:HaneD

我有一个产品搜索小部件,当您选择某些产品时,会显示一个隐藏字段。我想在我的小部件选项中设置一个自定义类别,然后用于显示隐藏字段。

当我将父category\\u id手动添加到代码中时,前端可以100%工作。现在我只需要在后端设置category选项。

这是我目前拥有的,但它不起作用,因为它没有存储所做的选择。我省略了搜索表单,因为它没有使用任何小部件选项。

class Equipment_Search extends WP_Widget {

    function Equipment_Search() {
        $widget_ops = array( \'classname\' => \'agriquip\', \'description\' => __(\'Displays the Equipment Search Form\', \'agriquip\') );
        $control_ops = array( \'width\' => 200, \'height\' => 350, \'id_base\' => \'agriquip-widget\' );
        $this->WP_Widget( \'agriquip-widget\', __(\'Equipment Search\', \'agriquip\'), $widget_ops, $control_ops );
    }

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

        $title = apply_filters(\'widget_title\', $instance[\'title\'] );
        $tract_id = isset( $instance[\'exc_equipment_cat\'] ) ? $instance[\'exc_equipment_cat\'] : false;   
        $tract = wp_list_pluck(get_terms(\'exc_equipment_cat\', array(\'parent\' => 3)), \'slug\');
        $tractparent = get_term_by(\'id\',\'3\', \'exc_equipment_cat\');
        $tractparent = $tractparent->slug; 


        echo $before_widget;
echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        //Strip tags from title and name to remove HTML 
        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'exc_equipment_cat\'] = $new_instance[\'exc_equipment_cat\'] ? 1 : 0;

        return $instance;
    }

    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( \'title\' => __(\'Equipment Search\', \'agriquip\'), \'exc_equipment_cat\' => \'-1\');
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'agriquip\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" class="widefat" />
        </p>

            <form method="post" action="">
                <label for="exc_equipment_cat">Category to use with Kw Options</label>
                <?php
                        $dropdown_args = array(
                            \'taxonomy\'          => \'exc_equipment_cat\',
                            \'name\'              => $this->get_field_name(\'exc_equipment_cat\'),
                            \'show_count\'        => 1,
                            \'orderby\'           => \'name\',
                            \'hierarchical\'      => true,
                            \'echo\'              => 0,
                            \'depth\'             => 1,
                            \'show_option_none\'  => \'Select Category\',
                            \'selected\'          => (int)$instance[\'exc_equipment_cat\'],
                            );
                        wp_dropdown_categories(apply_filters(\'widget_categories_dropdown_args\', $dropdown_args));
                        $select = wp_dropdown_categories(apply_filters(\'widget_categories_dropdown_args\', $dropdown_args));
                        $select = preg_replace("#<select([^>]*)>#", "<select$1 class=\'widefat\'>", $select);
                    echo $select;
                ?>  
            </form>


    <?php
    }

4 个回复
最合适的回答,由SO网友:HaneD 整理而成

我明白了:-)多亏了this

function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        //Strip tags from title and name to remove HTML 
        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'kwtax\'] = strip_tags( $new_instance[\'kwtax\'] );

        return $instance;
    }

    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( \'title\' => __(\'Equipment Search\', \'agriquip\'), \'kwtax\' => \'\');
        $instance = wp_parse_args( (array) $instance, $defaults );
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'agriquip\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" class="widefat" />
        </p>

          <p>
            <select id="<?php echo $this->get_field_id(\'kwtax\'); ?>" name="<?php echo $this->get_field_name(\'kwtax\'); ?>" class="widefat" style="width:100%;">
                <?php foreach(get_terms(\'exc_equipment_cat\',\'parent=0&hide_empty=0\') as $term) { ?>
                <option <?php selected( $instance[\'kwtax\'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
                <?php } ?>      
            </select>
        </p>

SO网友:tnchuntic

您可以使用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);
        ?>

SO网友:Richard Dinh

谢谢你,哈内德。

你的例子可能是我能找到的唯一有效的例子。如果其他人希望在不创建自定义类别的情况下使用它,请尝试替换:

<?php foreach(get_terms(\'exc_equipment_cat\',\'parent=0&hide_empty=0\') as $term) { ?>
使用

<?php foreach(get_terms(\'category\',\'parent=0&hide_empty=0\') as $term) { ?>

SO网友:tiltedtimmy

你离得太近了,你所要做的就是从更新方法中找到你的类别ID。像这样:

    $instance[\'exc_equipment_cat\'] = filter_var($new_instance[\'exc_equipment_cat\'], FILTER_SANITIZE_NUMBER_INT);
或者,您希望清理来自更新的数字。

这将为您提供类别或分类id。

结束

相关推荐

Remove Widgets in Dashboard

另一个noob问题。。。当作者登录并访问其仪表板时,他们会看到各种小部件,例如Internet Explorer警告、WordPress动态新闻。是否要为所有用户(现在和将来)删除所有这些内容,以便他们只看到“快速按”&;\'现在的小部件?谢谢