Php wp AJAX加载更多插件,重复前面的文章
我使用wp-ajax加载更多内容,同时单击按钮,重复上一篇文章。如何修复它。在这里,我在下面分享我的代码:Php wp AJAX加载更多插件,重复前面的文章,php,ajax,wordpress,load,Php,Ajax,Wordpress,Load,我使用wp-ajax加载更多内容,同时单击按钮,重复上一篇文章。如何修复它。在这里,我在下面分享我的代码: <?php $the_query = new WP_Query( array( 'posts_per_page'=>10,//on loading page i show 10 after click load more i want to show other posts 'post_type'=>'post-name',
<?php
$the_query = new WP_Query( array(
'posts_per_page'=>10,//on loading page i show 10 after click load more i want to show other posts
'post_type'=>'post-name',
'category_name' => 'A-E',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// here I print the following data
<?php
endwhile;
?>
<?php
echo do_shortcode('[ajax_load_more post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
?>
//这里我打印以下数据
我只是用这个来解决我自己的问题:
while ( $query->have_posts() ) : $query->the_post();
$do_not_duplicate[] = $post->ID; // Store post ID in array
// Other loop actions could go here
endwhile; wp_reset_query();
$post__not_in = ($do_not_duplicate) ? implode(',', $do_not_duplicate) : '';
echo do_shortcode('[ajax_load_more post__not_in="'. $post__not_in .'" post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
您不需要创建自定义查询。您正在使用一个插件,该插件将通过其短代码为您生成该插件。根据他们的文档,您只需为短代码创建设置
请参阅此处的说明
只需将生成的短代码粘贴到您的页面/帖子或php文件中,并使用do\u shortcode
该插件处理查询和每页的帖子等。如果要使用ajax加载更多的页面,必须使用offset参数。ajax加载更多
插件必须获得WP\u查询的下一篇文章。因此,WP_Query和ajax load more
都必须查询相同的页面。首先,通过添加order
和orderby
参数,将快捷码更改为与WP_查询相同:
[ajax_load_more post_type="post-name" posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
然后添加offset=(get\u query\u var('paged')?get\u query\u var('paged'):1)*10
如下所示:
[ajax_load_more post_type="post-name" offset='.((get_query_var('paged') ? get_query_var('paged') : 1)*10).' posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
注意:似乎ajax加载更多
不知道-
,因此必须通过,
将类别
或post\u类型
分开。除非你有一个名为a-e的类别,否则你不会通知插件你已经打印了10篇文章:)你是否缺少一些短码参数?这里:[ajax\u load\u more post\u type=“post name”posts\u per\u page=“10”category=“a-e”button\u label=“load more”],我想您还需要传递“paged”参数,这就是为什么不加载下一页,而总是第一页。对吗?请把你的短码贴出来。这就是您遇到的问题的基础。请参阅我在下面的@designtocode中添加的问题。我要求的php代码可以使[ajax\u load\u更]
。您是否已将其添加到functions.php文件中,或者它是另一个插件?它正在工作。我用另一种方式添加了post\u not\u in