Raja Ji
asked 8 years ago

My categories structure like this:

Cat 1
subCat 1
subCat 2
anothSubCat 1
anothSubCat 1
Cat 2
subCat 1
subCat 2
Cat 3

The categories are from custom post type and the current code looks like this:

`function getall() {
?>

jQuery(document).ready(function () {
jQuery(‘#getall’).change(function () {
var mainCat = jQuery(‘#getall’).val();
// call ajax
jQuery.ajax({
url: “/wp-admin/admin-ajax.php”,
type: ‘POST’,
data: ‘action=category_select&parentID=’ + mainCat,
success: function (results)
{
jQuery(“#getsub”).html(results);
}
});
});
});

<form action="/” method=”get”>

‘question-category’,
‘id’ => ‘question-category’,
‘taxonomy’ => ‘dwqa-question_category’,
‘show_option_none’ => __(‘Select category’, ‘text_domain’),
‘hide_empty’ => 0,
‘tabindex’ => 1,
‘orderby’ => ‘NAME’,
‘order’ => ‘DESC’,
‘selected’ => isset($_POST[‘catdropdown’]) ? $_POST[‘catdropdown’] : false,
));
?>

Select main category

Thats all, submit now !

<?php
}
die();
} // end if
}

add_action('wp_ajax_category_select', 'ajax_now');
add_action('wp_ajax_nopriv_category_select', 'ajax_now');`

I am able to get the parent categories list of the custom post type in the first drop-down. However, upon selecting the parent category, I am not getting the child category of the selected parent category in the dependent drop-down below.

1 Answers
Allen
answered 8 years ago

@Raja Ji : hi, so sorry for my late reply for you, if you’re talking about the plug-in DW Question & Answer, to display the subcategory, we’re using the fuction : wp_dropdown_categories()
you can open file : DW-Question-Answer/templates/question-submit-form.php, go to line 54, you can see that function
you can get more details of the function at this link : https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
For subcategory, they have an argument : 'depth' => 0, , the depth is the level of sub-category, you can change it as you want can be 2 levels as your example.
the 'taxonomy' argument is the taxonomy of custom post type you need.
the 'hierarchical' argument, if you set it to 1, the category will be display as sub-level style.
the 'hide_empty' argument, set to 0 to show all the category event it not have post in it.

Raja Ji
replied 8 years ago

Hi Allen,

I have tried ‘depth’ => 0, it displays the sub-categories in the same drop-down.
The functionality I am looking for is dependent drop down.
Once I select Parent category in the first dropdown, the sub-categories should be populated in the second drop down. Similar to this (http://demo.tutorialzine.com/2011/11/chained-ajax-selects-jquery/)

Raja Ji
replied 8 years ago

Hi Allen,

I have tried ‘depth’ => 0, it displays the sub-categories in the same drop-down.
The functionality I am looking for is dependent drop down.
Once I select Parent category in the first dropdown, the sub-categories should be populated in the second drop down. Similar to this (http://demo.tutorialzine.com/2011/11/chained-ajax-selects-jquery/)

Raja Ji
replied 8 years ago

Hi Allen,

I have tried ‘depth’ => 0, it displays the sub-categories in the same drop-down.
The functionality I am looking for is dependent drop down.
Once I select Parent category in the first dropdown, the sub-categories should be populated in the second drop down. Similar to this (http://demo.tutorialzine.com/2011/11/chained-ajax-selects-jquery/)

Powered by DW Question & Answer Pro