在下拉搜索中显示自定义类别层次结构

时间:2019-01-29 15:14:07

标签: wordpress hierarchy custom-taxonomy

我使用的是Wordpress,并有一个搜索框,其中显示自定义类别的列表作为下拉字段。

当我在后端添加类别时,它们会显示其层次结构,但是,在下拉字段中,它们只会显示为字母列表。

即:

后端

A1
A2
-A2.1
-A2.2
A3
A4

前端

A1
A2
A2.1
A2.2
A3
A4

仅当将某项分配给该类别时才显示下拉搜索中的类别,这很好,但是如何获取前端以显示层次结构?

我认为我需要在代码中添加wp_dropdown_categories,但是我不确定如何修改代码。

用于显示类别的下拉框的代码如下,但我还提供了完整代码:

<div class="four columns">
                    <select id="adv_filter_city" name="adv_filter_city" class="fpcselector">
                        <option value="">-- <?php _e('Area', THE_LANG);?> --</option>
                        <?php
                            foreach ($fpc_property_cities as $fpc_property_city) {
                                $fpc_cityslug   = $fpc_property_city->slug;
                                $fpc_cityname   = $fpc_property_city->name;

                                if($fpc_cityslug==$filter_city){
                                    $optselected = 'selected="selected"';
                                }else{
                                    $optselected = '';
                                }
                                echo '<option value="'.esc_attr( $fpc_cityslug ).'" '.$optselected.'>'.$fpc_cityname.'</option>';
                            }
                        ?>
                    </select>
                </div>
            </div>

完整代码

<?php
class FPC_PropertySearchWidgetHome extends WP_Widget {
    /** constructor */

    function FPC_PropertySearchWidgetHome() {
        $widget_ops = array('classname' => 'widget_fpc_property_search_home', 'description' => __(' Home Property Search',THE_LANG) );
        $this->WP_Widget('fpc-property-search-home', __('Home Property Search',THE_LANG), $widget_ops);
    }


  /** @see WP_Widget::widget */
    function widget($args, $instance) {     
        extract( $args );
        $title = apply_filters('widget_title', empty($instance['title']) ? __('Property Search',THE_LANG) : $instance['title']);

        global $wp_query;

        $fpc_initial = THE_INITIAL;
        $fpc_shortname = THE_SHORTNAME;

        $fpc_filterMap  =  fpc_get_option( $fpc_shortname . '_filter_map');
        $fpc_minprice = fpc_get_option( $fpc_shortname . '_min_price','0');
        $fpc_maxprice = fpc_get_option( $fpc_shortname . '_max_price','1000000');
        $fpc_cursymbol = fpc_get_option( $fpc_shortname . '_currency_symbol');
        $fpc_curplace = fpc_get_option( $fpc_shortname . '_currency_place');

        if(!is_numeric($fpc_minprice)){
            $fpc_minprice = 0;
        }
        if(!is_numeric($fpc_maxprice)){
            $fpc_maxprice = 1000000;
        }

        $fpc_search_page = fpc_get_propsearch_page();
        $fpc_adv_submit = $fpc_search_page['submiturl'];
        $fpc_page_id = $fpc_search_page['pageid'];

        $icons          =   array();
        $fpc_purposecat     =   'property_purpose';
        $fpc_purpose_terms  =   get_terms($fpc_purposecat);
        $fpc_propertycat    =   'property_category';
        $fpc_property_cats  =   get_terms($fpc_propertycat, array( 'exclude' => '5' ) );
        $fpc_propertycity   =   'property_city';
        $fpc_property_cities=   get_terms($fpc_propertycity);

        $fpc_propstatuses = fpc_get_option($fpc_shortname.'_property_status');
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>


                                <!-- Advanced Search Container -->
                               <div class="fpc-widget-prop-search">
                               <?php
                               $filter_keywords = isset($_REQUEST['adv_filter_keywords'])? $_REQUEST['adv_filter_keywords'] : '';
                               $filter_purpose = isset($_REQUEST['adv_filter_purpose'])? $_REQUEST['adv_filter_purpose'] : '';
                               $filter_type = isset($_REQUEST['adv_filter_type'])? $_REQUEST['adv_filter_type'] : '';
                               $filter_city = isset($_REQUEST['adv_filter_city'])? $_REQUEST['adv_filter_city'] : '';
                               $filter_status = isset($_REQUEST['adv_filter_status'])? $_REQUEST['adv_filter_status'] : '';
                               $filter_numroom = isset($_REQUEST['adv_filter_numroom'])? $_REQUEST['adv_filter_numroom'] : '';
                               $filter_numbath = isset($_REQUEST['adv_filter_numbath'])? $_REQUEST['adv_filter_numbath'] : '';
                               $filter_size_min = isset($_REQUEST['adv_filter_size_min'])? $_REQUEST['adv_filter_size_min'] : '';
                               $filter_price_min = isset($_REQUEST['adv_filter_price_min'])? $_REQUEST['adv_filter_price_min'] : $fpc_minprice;
                               $filter_price_max = isset($_REQUEST['adv_filter_price_max'])? $_REQUEST['adv_filter_price_max'] : $fpc_maxprice;
                               ?>
                                    <div class="advanced-search">
                                    <form id="frmwdgsearch" class="frmadvsearch" method="get" action="<?php echo esc_url( $fpc_adv_submit ); ?>">
                                        <div class="form-search container">
                <div class="row search-row-1">
                    <div class="four columns"><input type="text" class="fpctextbox" name="adv_filter_keywords" id="adv_filter_keywords" value="<?php echo esc_attr($filter_keywords); ?>" placeholder="<?php esc_attr_e('Search Property Name', THE_LANG); ?>" /></div>


                                                <div class="four columns">
                                                    <select id="wdg_filter_status" name="adv_filter_status" class="fpcselector">
                                                        <option value="">-- <?php _e('Any Status', THE_LANG);?> --</option>
                                                        <?php
                                                        $fpc_optpropstatus = array();
                                                        for($i=0;$i<count($fpc_propstatuses);$i++){
                                                            $fpc_propstatus = $fpc_propstatuses[$i];
                                                            $fpc_optpropstatus[$fpc_propstatus] = $fpc_propstatus;

                                                            if($fpc_propstatus==$filter_status){
                                                                $optselected = 'selected="selected"';
                                                            }else{
                                                                $optselected = '';
                                                            }
                                                            echo '<option value="'. esc_attr( $fpc_propstatus ).'" '.$optselected.'>'.$fpc_propstatus.'</option>';
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                    <div class="four columns">
                        <select id="adv_filter_city" name="adv_filter_city" class="fpcselector">
                            <option value="">-- <?php _e('Area', THE_LANG);?> --</option>
                            <?php
                                foreach ($fpc_property_cities as $fpc_property_city) {
                                    $fpc_cityslug   = $fpc_property_city->slug;
                                    $fpc_cityname   = $fpc_property_city->name;

                                    if($fpc_cityslug==$filter_city){
                                        $optselected = 'selected="selected"';
                                    }else{
                                        $optselected = '';
                                    }
                                    echo '<option value="'.esc_attr( $fpc_cityslug ).'" '.$optselected.'>'.$fpc_cityname.'</option>';
                                }
                            ?>
                        </select>
                    </div>
                </div>
                                            <div class="row search-row-2">

                                                    <div class="four columns">
                        <select id="adv_filter_type" name="adv_filter_type" class="fpcselector">
                            <option value="">-- <?php _e('Property Type', THE_LANG);?> --</option>
                            <?php
                                foreach ($fpc_property_cats as $fpc_property_cat) {
                                    $fpc_catslug    = $fpc_property_cat->slug;
                                    $fpc_catname    = $fpc_property_cat->name;

                                    if($fpc_catslug==$filter_type){
                                        $optselected = 'selected="selected"';
                                    }else{
                                        $optselected = '';
                                    }
                                    echo '<option value="'.esc_attr( $fpc_catslug ).'" '.$optselected.'>'.$fpc_catname.'</option>';
                                }
                            ?>
                        </select>
                    </div>

                                              <div class="four columns"><input type="text" class="fpctextbox" pattern="[0-9]+" id="adv_filter_numroom" title="Please enter whole numbers"  name="adv_filter_numroom" value="<?php echo esc_attr($filter_numroom); ?>" placeholder="<?php esc_attr_e('Min Bedrooms', THE_LANG); ?>" /></div>



                 <div class="four columns"><input type="text" class="fpctextbox" pattern="[0-9]+" id="adv_filter_size_min" title="Please enter whole numbers" name="adv_filter_size_min" value="<?php echo esc_attr($filter_size_min); ?>" placeholder="<?php esc_attr_e('Min Acres', THE_LANG); ?>" /></div>




                    <div class="twelve columns rangeslidercontainer">
                        <div class="rangeslider"></div>
                        <div class="rangetext"><?php echo $fpc_cursymbol; ?><span id="text_price_min" class="text_price_min"><?php echo esc_attr($filter_price_min); ?></span> - <?php echo $fpc_cursymbol; ?><span id="text_price_max" class="text_price_max"><?php echo esc_attr($filter_price_max); ?></span></div>
                        <input type="hidden" name="adv_filter_price_min" id="adv_filter_price_min" class="adv_filter_price_min" value="<?php echo esc_attr($filter_price_min); ?>" />
                        <input type="hidden" name="adv_filter_price_max" id="adv_filter_price_max" class="adv_filter_price_max" value="<?php echo esc_attr($filter_price_max); ?>" />
                    </div>
                </div>
            </div>
                                        <div class="container">
                                            <div class="row">
                                                <div class="four columns"><input name="submit" type="submit" class="button" id="wdg_filter_submit" value="<?php _e('Search',THE_LANG);?>"></div>
                                            </div>
                                        </div>
                                        <input name="page_id" id="fpc_page_id" type="hidden" value="<?php echo esc_attr( $fpc_page_id ); ?>" />
                                    </form>
                                    </div>
                               </div>
                               <!-- END Advanced Search Container -->



              <?php echo $after_widget; ?>

        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {             
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
        $instance['title'] = (isset($instance['title']))? $instance['title'] : "";
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo esc_attr( $this->get_field_id('title') ); ?>"><?php _e('Title:', THE_LANG); ?> <input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
        <?php 
    }

} // class  Widget
?>

非常感谢

0 个答案:

没有答案