Quantcast
Channel: Ajax Load more CPT via shortcode - WordPress Development Stack Exchange
Viewing all articles
Browse latest Browse all 4

Ajax Load more CPT via shortcode

$
0
0

I have a problem with ajax load more for my CustomPostType. I think i have mistake but don't know where. If it possible to edit my code, i will happy.

UPDATE

I have create div cool to paste in it loaded more post. After clicking load more i have 0 in div cool. What does it mean?

In functions.php

    add_shortcode( 'list-posts-basic', 'rmcc_post_listing_shortcode1' );

function rmcc_post_listing_shortcode1( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'imggal',
        'order' => 'ASC',
        'orderby' => 'title',
        'posts_per_page' => 1,
    ) );


    if ( $query->have_posts() ) { ?>
        <ul id="ajaxx" class="clothes-listing">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php endwhile;
            wp_reset_postdata(); ?>
            <div id="cool"></div>
            <a id="more_posts" href="#">Load More</a>
        </ul>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}

add_action('wp_ajax_nopriv_rmcc_post_listing_shortcode1', 'rmcc_post_listing_shortcode1'); 
add_action('wp_ajax_rmcc_post_listing_shortcode1', 'rmcc_post_listing_shortcode1');

AJAX

   (function($) {  
$(document).ready(function($) {   
    $("#more_posts").on("click",function(e){ // When btn is pressed.
    e.preventDefault();


        $.ajax({  
        url: ajax_object.ajax_url,  
        data: {  
              'action' : 'rmcc_post_listing_shortcode1'  
          },   
        success: function (data) {  
              if (data.length > 0) {  
                $('#cool').html(data);  
              }
          },
      });

   });

   });    
})(jQuery);

Please help me!


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images