首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在前端分类表单中显示高级自定义字段插件字段

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

Stack Overflow用户
提问于 2014-03-04 21:00:29
回答 1查看 1.5K关注 0票数 0

我正在使用word press主题分类新闻,现在我想添加多个动态下拉菜单,这些下拉菜单彼此相继。像国家下拉,当任何国家被选择时,另一个下拉列表显示选择的国家cities.for这个im使用高级自定义字段插件,它在后端工作,但我不知道如何在前端显示这些字段。我正在尝试此链接,但无法帮助http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/

EN

回答 1

Stack Overflow用户

发布于 2014-03-04 21:49:15

代码语言:javascript
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22172662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档