Wordpress - how can I fetch more posts via AJAX?

前端 未结 1 1737
盖世英雄少女心
盖世英雄少女心 2020-12-09 00:31

To begin with, I think this is a longshot but hopefully there is someone out there that can help.

To explain the current situation, at the moment I have a custom plu

相关标签:
1条回答
  • 2020-12-09 01:13

    After a lot of effort, I found the answer, here is a solution for those stuck in the same position.

    This goes in your plugin page where you are getting posts for a user etc.

    <script type="text/javascript">
        $(document).ready(function() {
    
            posts = 8;
            author_posts = parseInt(<?php echo $author_posts; ?>);
    
            $("#link_selector").click(function() {
    
                // alert('posts - ' + posts + 'author posts - ' + author_posts);
    
    
                if ((posts - author_posts) > 3) {
                    $("#link_selector").text('No more posts!');
                }
                else {
    
                    var category        = '<?php echo strtolower($category); ?>';
                    var id              = '<?php echo $blogger_id; ?>';
                    var firstname       = '<?php echo $firstname; ?>';
                    var surname         = '<?php echo $surname; ?>';
    
    
                    // alert(posts + category + id + firstname + surname);
    
                    $.ajax({
                        type: "GET",
                        url: "/wordpress/wp-admin/admin-ajax.php",
                        dataType: 'html',
                        data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}),
                        success: function(data){
                            $('#ajax_results_container').hide().fadeIn('slow').html(data);
                            posts = posts + 4;
    
                            if ((posts - author_posts) > 3) {
                                $("#link_selector").text('No more posts!');
                            }
    
                        }
                    });
                }
    
            });
        });
    
        </script>       
    

    Then in your theme's functions.php, put your function to do your ajax request:

    function loadMore() {
    
        $blogger_id = $_GET['id'];
        // get your $_GET variables sorted out
    
        // setup your query to get what you want
        query_posts('posts_per_page=' . $posts . '&author='.$blogger_id);
    
        // initialsise your output
        $output = '';
    
        // the Loop
        while (have_posts()) : the_post();
    
            // define the structure of your posts markup
    
        endwhile;
    
        // Reset Query
        wp_reset_query();
    
        die($output);
    
    }
    

    Then, just after the closing of your function, put in the action hooks

    add_action('wp_ajax_loadMore', 'loadMore');
    add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed
    

    That's it!

    0 讨论(0)
提交回复
热议问题