Wordpress - how can I fetch more posts via AJAX?

前端 未结 1 1748
盖世英雄少女心
盖世英雄少女心 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.

           
    

    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)
提交回复
热议问题