WordPress-如何通过AJAX获取更多帖子?

WordPress-如何通过AJAX获取更多帖子?,第1张

WordPress-如何通过AJAX获取更多帖子?

经过大量的努力,我找到了答案,这是为陷入困境的人提供的解决方案。

这进入您的插件页面,您将在其中获取用户的帖子等。

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

然后在主题的functions.php中,将函数放入您的ajax请求中:

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);}

然后,在函数关闭后,放入动作钩子

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

而已!



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5089148.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存