【问题标题】:how can i show advanced custom fields plugin fields show in front end classipress form我如何在前端 classipress 表单中显示高级自定义字段插件字段
【发布时间】:2014-03-04 21:49:14
【问题描述】:

我正在使用 word press 主题 classipress,现在我想添加多个相互中继的动态下拉列表。 like country drop down,when any country is selected another drop down appears that selected country cities.for this im using advanced custom fields plugin, 它适用于后端,但我不知道如何在前端显示这些字段。 我正在尝试这个链接,但无法帮助http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/

【问题讨论】:

标签: php wordpress


【解决方案1】:
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.

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多