doumi9618 2018-12-04 18:13
浏览 57

Wordpress下降孩子的类别孩子

I am new to programming and i am trying to create 4 level drop down. As of now i am referring to the link here.

With the above link, i can only create 2 level drop down. Since my programming skills are on the beginner side, i am not sure how to achieve this.

I have created wordpress categories like this:

Parent Category > Child 1 Category > Child 2 Category > Child 3 Category

There should be 4 Drop Down:

Parent Drop Down

Child1 Drop Down

Child2 Drop Down

Child 3 Drop Down

Once the user selects all the drop down, it should redirect to a post with customize links as follows:

http://www.example.com/parent/child1/child2/child3/

Is it possible to create 4 level drop down with customized redirection?

  • 写回答

1条回答 默认 最新

  • doulu8341 2018-12-04 19:17
    关注

    Well, i have tried to add the below code into functions.php. The first drop down is working, when i select the second drop down, it does not auto-populate the third drop down.

    Here is the code which i have tried so far.

    if ( ! class_exists( 'frontendAjaxDropdown' ) ):
     class frontendAjaxDropdown
     {
     /**
     * Loading WordPress hooks
     */
     function __construct()
     {
     /**
     * Add shortcode function
     */
     add_shortcode( 'ajax-dropdown', array($this, 'init_shortocde') );
     /**
     * Register ajax action
     */
     add_action( 'wp_ajax_get_subcat1', array($this, 'getSubCat1') );
     /**
     * Register ajax action for non loged in user
     */
     add_action('wp_ajax_nopriv_get_subcat1', array($this, 'getSubCat1') );
     
     
     add_action( 'wp_ajax_get_subcat2', array($this, 'getSubCat2') );
     /**
     * Register ajax action for non loged in user
     */
     add_action('wp_ajax_nopriv_get_subcat2', array($this, 'getSubCat2') ); 
     
     
     }
     /**
     * Show parent dropdown for wordpress category and loaded necessarry javascripts
     */
     function init_shortocde()
     {
     wp_dropdown_categories(
     'name=main_cat&selected=-1&hierarchical=1&depth=1&hide_empty=0&show_option_none=All Categories'
     );
     ?>
     <script type="text/javascript">
     (function($){
     $("#main_cat").change(function(){
     $("#sub_cat1").empty();
     $("#sub_cat2").empty();
     $.ajax({
     type: "post",
     url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
     data: { action: 'get_subcat1', cat_id1: $("#main_cat option:selected").val() },
     beforeSend: function() {$("#loading").fadeIn('slow');},
     success: function(data) {
     $("#loading").fadeOut('slow');
     $("#sub_cat1").append(data);
     $("#sub_cat2").empty();
     }
     });
     });
     })(jQuery);
     </script>
    
    <div id="loading" style="display: none;">Loading...</div>
     <div id="sub_cat1"></div>
     <?php
     }
     /**
     * AJAX action: Shows dropdown for selected parent
     */
     function getSubCat1()
     {
     wp_dropdown_categories(
     "name=sub_cat1&selected=-1&hierarchical=1&depth=1&hide_empty=0&child_of={$_POST['cat_id1']}"
     );
     }
     }
    ?>
    
    
     <script type="text/javascript">
     (function($){
     $("#sub_cat1").change(function(){
     $("#sub_cat2").empty();
     $.ajax({
     type: "post",
     url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
     data: { action: 'get_subcat2', cat_id2: $("#sub_cat1 option:selected").val() },
     beforeSend: function() {$("#loading").fadeIn('slow');},
     success: function(data) {
     $("#loading").fadeOut('slow');
     $("#sub_cat2").append(data);
     }
     });
     });
     })(jQuery);
     </script>
    
     
     
     <div id="loading1" style="display: none;">Loading...</div>
     <div id="sub_cat2"></div>
     
     <?php
      
    
     
     function getSubCat2()
     {
     wp_dropdown_categories(
     "name=sub_cat2&selected=-1&hierarchical=1&depth=1&hide_empty=0&child_of={$_POST['cat_id1']}+{$_POST['cat_id2']}"
     );
     
     
     die();
     }
    
    endif;
    new frontendAjaxDropdown();

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写