Wordpress分类标准ajax链选择?

时间:2014-03-11 05:41:18

标签: javascript php jquery ajax wordpress

我的英语非常抱歉,ı使用谷歌翻译:)我的wordpress主题自定义帖子类型名称otomobil,分类名称arac_model ...ı想1.selectbox make audi bmw wolksvagen 2.selectbox in audi a3 bmw 3.20 ... 。这个怎么做的? ı使用此网站代码http://www.1stwebdesigner.com/css/implement-ajax-wordpress-themes/ 我的代码:

1.selectbox

<?php
wp_dropdown_categories('class=select_styled&show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Marka Seçin&name=main_cat&post_type=otomobil&taxonomy=arac_model');
?>

2.selectbox

<select class="select_styled" name="sub_cat" id="sub_cat" disabled="disabled"></select>

jquery代码:

<script type="text/javascript">
$(function(){
            $('#main_cat').change(function(){
                    var $mainCat=$('#main_cat').val();

                    // call ajax
                     $("#sub_cat").empty();
                        $.ajax({
                            url:"<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php",
                            type:'POST',
                            data:'action=my_special_action&main_catid=' + $mainCat,
                            success:function(results)
                                 {
                                //  alert(results);
                $("#sub_cat").removeAttr("disabled");
                $("#sub_cat").append(results);
                                        }
                                   });
                          }
                        );
});
</script> 

functions.php代码:

add_action('wp_ajax_my_special_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_special_action', 'my_action_callback');
function implement_ajax() {
if(isset($_POST['main_catid']))
            {
            $categories=  get_categories('class=select_styled&child_of='.$_POST['main_catid'].'&hide_empty=0&post_type=otomobil&taxonomy=arac_model');
              foreach ($categories as $cat) {
                $option .= '<option value="'.$cat->term_id.'">';
                $option .= $cat->cat_name;
                $option .= ' ('.$cat->category_count.')';
                $option .= '</option>';
              }

              echo '<option value="-1" selected="selected">Scegli...</option>'.$option;
            die();
            } // end if
}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');//for users that are not logged in.

0 个答案:

没有答案