2014-03-04 23 views
0

我使用word按主題classipress,現在我想要添加多個動態下拉相互接力。 像國家下拉菜單,當選擇任何國家的另一個下拉菜單出現選定的國家cities.for這即時通訊使用高級自定義字段插件, 它在後端工作,但我不知道如何在前端顯示這些領域。 我想這個鏈接,但不能幫忙http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/我如何顯示高級自定義字段插件字段顯示在前端classipress形式

+0

你必須張貼您正在使用的代碼。請參閱[問]和[如何包含最小示例](http://stackoverflow.com/help/mcve)。 – brasofilo

回答

0
For dynamic drop down you need to use ajax with wordpress 

I am explain all step which help you to create dynamic drop down country with city. 

Please follow below steps: 
Step : 1 
At first, Create Categories: Parent and Sub category from wp admin. 
Step : 2 

Create a template in WordPress(I am not going in details of what are the templates of WordPress and how they process in WordPress themes) in which we will implement ajax functionality. Open a new php file and save it with any name like I saved it with implement-ajax.php 
Add the following code in the newly created page. 

<?php 
/* 
Template Name: Implement Ajax 
*/ 
get_header(); 
?> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script> 

<style type="text/css"> 
#content{width:auto; height:400px; margin:50px;} 
</style> 
<div id="content"> 
<?php 
wp_dropdown_categories('show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Main Categories&name=main_cat'); 
?> 
<select name="sub_cat" id="sub_cat" disabled="disabled"></select> 
</div> 
<?php 
get_footer(); 
?> 


Step :3 

Now add jquery code in template 

$(function(){ 
    $('#main_cat').change(function(){ 
    var $mainCat=$('#main_cat').val(); 
    // call ajax 
    $("#sub_cat").empty(); 
    $.ajax({ 
    url:"bloginfo('wpurl'); ?>/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); 
     } 
     }); 
    } 
); 
}); 


Step:6 Now add this code in function.php 


function implement_ajax() { 
if(isset($_POST['main_catid'])) 
      { 
      $categories= get_categories('child_of='.$_POST['main_catid'].'&hide_empty=0'); 
       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(); 
      `enter code here`} // end if 
} 
add_action('wp_ajax_my_special_action', 'implement_ajax'); 
add_action('wp_ajax_nopriv_my_special_action', 'implement_ajax');//for users that are not logged in. 

Step :5 

Now create a new page in the dashboard and assign the template to it. 
相關問題