栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

而已!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427394.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号