Ajax load custom post type but always appearance “0”
-
I want to get custom post type list by ajax code, but it always appearance “0” on html.
here is my code:<div class="daily_select"> <label> Ch?n T?nh Thành </label> <?php wp_dropdown_categories( array( 'show_option_all' => '', 'hierarchical' => 1, 'taxonomy' => 'daily_cat', 'hide_if_empty' => 0, 'name' => 'cat', 'id' => 'daily_cat',)); ?> </div> <script type="text/javascript"> (function($){ $("#daily_cat").change(function(){ var sub_id = $("#daily_cat option:selected").val(); $("#daily_listitem").empty(); $("#loading").fadeIn('slow'); $.ajax({ type: "post", url: "<?php echo admin_url( 'admin-ajax.php' ); ?>", data: { action: 'get_daily', sub_id: sub_id }, beforeSend: function() {$("#loading").fadeIn('slow');}, success: function(data) { $("#loading").fadeOut('slow'); $("#daily_listitem").append(data); } }); }); })(jQuery); </script> <div id="loading" style="display: none;text-align:center;color:red">Loading...</div> <div id="daily_listitem"> </div>
And this is my function
function get_daily() { if(!isset($_POST['sub_id'])){$_POST['sub_id']=21;} $args = array( 'post_type' => 'daily', 'tax_query' => array( array( 'taxonomy' => 'daily_cat', 'field' => 'term_id', 'terms' => $_POST['sub_id'] ) ) ); ?> <ul class="listitem"> <?php $my_query = new WP_Query( $args ); if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();?> <li class="daily-item" data-map="<?php the_field('test'); ?>"> <h5><?php the_title(); ?></h5> <div class="daily-item-body"><?php the_content(); ?></div> </li> <?php endwhile; endif; ?></ul> <?php } add_action( 'wp_ajax_get_daily', 'get_daily' ); add_action('wp_ajax_nopriv_get_daily', 'get_daily');
Thank all !
[Moderator note: code fixed. Please wrap code in the backtick character or use the code button.
Only images from certain white listed sites will appear here. The URL must end in a proper image filename, thus URLs from Google Images will not work. I suggest you post images on imgur.com if you want them to appear here]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Ajax load custom post type but always appearance “0”’ is closed to new replies.