如何在前端classipress表单中显示高级自定义字段插件字段

时间:2014-03-04 13:00:29

标签: php wordpress

我正在使用word press主题classipress,现在我想添加多个动态下拉列表,它们相互转发。 像国家/地区下拉,当选择任何国家/地区时,另一个下拉列表会显示所选的国家/地区城市。为此我使用高级自定义字段插件, 它适用于后端,但我不知道如何在前端显示这些字段。 我正在尝试此链接但无法帮助http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/

1 个答案:

答案 0 :(得分: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.