• Resolved jrmyp

    (@jrmyptrs)


    Hello,

    I’d like to use this plugin so that on my single.php view, I can use Ajax Load More shortcode to display a list of the next oldest posts in chronological order/by date in an infinite scroll.

    I did see that there is a Previous Post add-on, but from what I understand, this add-on just fully loads the next article itself and updates the URL on a post-by-post basis, rather than loading in multiple posts at a time using the repeater template I’ve already setup for the home page.

    I’m having trouble deciphering if this is possible from the Documentation and the WP Support forums, so I’d sincerely appreciate any help or guidance.

    • This topic was modified 9 years, 4 months ago by jrmyp.
    • This topic was modified 9 years, 4 months ago by jrmyp.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    Hi @jrmyptrs,
    You can do this without the Previous Post add-on. That is specifically for displaying the entire single post.

    Just drop the echo do_shortcode('[ajax_load_more]'); shortcode into your single template and you can load additional posts.

    Hope this helps.
    Cheers,

    Thread Starter jrmyp

    (@jrmyptrs)

    Fantastic, thank you so much for that Darren.

    One tip I’d add for anyone who else who comes across this, just be sure to include the “offset” parameter in your shortcode to avoid showing the current post in the loop, e.g.:

    echo do_shortcode('[ajax_load_more offset='1']');

    Thread Starter jrmyp

    (@jrmyptrs)

    Actually – scratch that, the offset appears to only work for the very first post.

    @dcooney, is there something I can do to make that loop always exclude the current post and any posts that are newer than the current post?

    • This reply was modified 9 years, 4 months ago by jrmyp.
    Plugin Author Darren Cooney

    (@dcooney)

    You’ll need to pass the current post id to the shortcode.
    Have a look at the post__not_in parameter or the excluding posts example inside the plugin.

    Cheers,

    Thread Starter jrmyp

    (@jrmyptrs)

    Thanks for all your help @dcooney, I was able to remove the current post by using the get_the_ID(); function and storing that as a variable as your excluding posts example showed.

    Any suggestions on how to filter out any posts that are newer than the current single post?

    Thread Starter jrmyp

    (@jrmyptrs)

    Posting the code solution for anyone else who needs to do something similar.

    First, you’ll need to go into your functions.php file and add the following function:

    function my_alm_query_args_date_after($args){
        
        if(isset($args['the_date'])){
            $date = date('Y-m-d H:i:s', $args['the_date']); // Get date timestamp and convert to readable format
            
            $args['date_query'] = array(
                array(
                    'before' => $date,
                ),
            );
            
        }
       return $args;
    }
    add_filter( 'alm_query_args_date_after', 'my_alm_query_args_date_after');

    Then, in single.php, the shortcode:

    <?php 
    $current = get_the_ID();
    $postDate = get_the_date('Y-m-d H:i:s');
    $date = strtotime($postDate);
    echo do_shortcode('[ajax_load_more id="date_after" custom_args="the_date:'. $date .'" exclude="'.$current.'"]'); 
    ?>

    Works like a charm.

    • This reply was modified 9 years, 4 months ago by jrmyp.
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘From Single, Load Next Posts Chronologically’ is closed to new replies.