I have question.
I would like to load post content, custom post, depends on category.
So first of all I would like to have drop down menu which will list all my categories then once user will select category list of post title will appear in second drop down (post from that particular category) then once that title selected post content will load. All that needs to be done on one page and without reloading so using Ajax. I stacked at the very beginning
<script type="text/javascript">
$(function(){
$('#main_cat').change(function(){
var $mainCat=$('#main_cat').val();
// call ajax
$("#sub_cat").empty();
$.ajax({
url:"/wp-admin/admin-ajax.php",
type:'POST',
data:'action=my_special_action&main_catid=' + $mainCat,
success:function(results)
{
// alert(results);
$("#sub_cat").removeAttr("style");
$("#sub_cat").append(results);
}
});
}
);
});
</script>
And I found that bit which was inserted into function.php and I do realize that that needs to be modified somehow to pull out posts rather then children of categories
add_action('wp_ajax_my_special_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_special_action', 'my_action_callback');
function implement_ajax() {
if(isset($_POST['main_catid']))
{
$categories= get_term_by('child_of='.$_POST['main_catid'].'&hide_empty=0');
foreach ($categories as $cat) {
$option .= '<option value="'.$cat->term_id.'">';
$option .= $cat->term_id;
$option .= ' ('.$cat->category_count.')';
$option .= '</option>';
}
echo '<option value="-1" selected="selected">Scegli...</option>'.$option;
die();
} // end if
}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');//for users that are not logged in.
Thats as far as i got.
Any help will be appreciated
Here, i have develop a simple demo for get a post data of depends on category without loading page using Ajax and jQuery.
Here is a main category, sub category and post dropdowns :
Now jQuery part: get a change events of dropdowns and retrieve data using ajax action.
frontend_ajax_object is a object of ajax url. you have to change according as you defined.
Now, here is a ajax function which is used to get a data based on selected fields.
I hope this demo will helps you. i have tested and it is working fine. let me know if this is helps you!
Thank you!