Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Cai for Ajax Load more CPT via shortcode

Your first problem is that your AJAX function in functions.php should echo its results, not return. You also need a call to wp_die() at the end.

I'm not completely sure what you are trying to do but it looks like half of your loop probably shouldn't be there. Currently you are outputing the whole #ajaxx list inside itself every time you click #load-more. Which at the very least will give you problems with duplicate IDs.

I suspect what you want is:

whatever-page-template.php

// Rest of page template and beginning of loop

<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>

// etc...

functions.php

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() ) : 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; endif;
    wp_reset_postdata();

    $myvariable = ob_get_clean();
    echo $myvariable;

    wp_die();
}

You are also always loading the same post so you need to implement some kind of pagination, but I would get the basic AJAX call fixed first!

You can read more about AJAX in Wordpress (including debugging!) here:


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>