[英]How do i check if a wordpress query has more posts?
So i am using ajax to filter and load posts into a container.所以我使用 ajax 过滤帖子并将其加载到容器中。 I want to limit the amount of posts to 6 at a time and add a load more button underneath if there are more than 6 posts, but i don't want to add pages because i have a few containers on the same page that i'm using this same treatment for and my understanding is pages would add a /page-1 or something like that to the url (am i wrong?).我想一次将帖子数量限制为 6 个,如果帖子超过 6 个,则在下方添加一个加载更多按钮,但我不想添加页面,因为我在同一页面上有几个容器我使用相同的处理方式,我的理解是页面会在 url 中添加一个 /page-1 或类似的东西(我错了吗?)。
Either way, i just want to know how to check if there are more posts that fit this criteria so i can show the load more button, and then when the load more button fires how do i just load 6 more.无论哪种方式,我只想知道如何检查是否有更多符合此条件的帖子,以便我可以显示加载更多按钮,然后当加载更多按钮触发时,我如何再加载 6 个。 Do i have to keep a page variable somewhere?我必须在某处保留一个页面变量吗? or is there another smarter way.或者有另一种更聪明的方法。
Here is my query这是我的查询
function ajax_filter_get_posts( $category, $tag )
{
$category = $_POST['category'];
$tag = $_POST['tag'];
if($category)
{
$category_args = array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category
);
}
$args = array(
'posts_per_page' => 6,
'post_status' => 'publish',
'tag' => implode(",",$tag),
'tax_query' => array(
$category_args,
),
'category__in' => array(187,186,183,182,184),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$output = post_factory($post);
$result['response'][] = $output;
$result['status'] = 'success';
endwhile; else:
$result['response'] = '<h2>No posts found</h2>';
$result['status'] = '404';
endif;
$result = json_encode($result);
echo $result;
die();
}
I know this post is about a year old, but in case anyone else needs an answer, here's the solution I'm implementing.我知道这篇文章已经发布了大约一年,但如果其他人需要答案,这是我正在实施的解决方案。 I'm using Ajax to pull in a wp_query from a PHP file.我正在使用 Ajax 从 PHP 文件中提取 wp_query。 The output from that file replaces the content of a div on my page.该文件的输出替换了我页面上 div 的内容。 This is only the relevant code, not my complete code.这只是相关代码,不是我的完整代码。
On click of the load more button, I use jQuery to count the number of posts on the page and put that count into a variable.单击加载更多按钮时,我使用 jQuery 计算页面上的帖子数并将该计数放入一个变量中。 Posts have a unique class assigned to them.帖子具有分配给它们的唯一类。
current = $('.item').length;
I send the post count to the PHP file using Ajax.我使用 Ajax 将帖子计数发送到 PHP 文件。
$.get(functions_home.url, { offset: current }, function(data) {
$('.grid').html(data);
}
I pull the post count from jQuery into a variable in the PHP file, and then use that variable to set the wp_query "offset" in $args.我将帖子计数从 jQuery 提取到 PHP 文件中的一个变量中,然后使用该变量在 $args 中设置 wp_query“偏移量”。
$offset = $_GET['offset'];
$args = array (
'offset' => $offset
);
Once the query ($query) runs, I use $query->found-posts to tell me the total number of relevant posts, and I put that in a variable.查询 ($query) 运行后,我使用 $query->found-posts 告诉我相关帖子的总数,并将其放入一个变量中。
$total = $query->found_posts;
I make a div in my PHP file and use the found posts variable so that it's populated with the total number of relevant posts.我在我的 PHP 文件中创建一个 div 并使用找到的帖子变量,以便用相关帖子的总数填充它。 I use CSS to hide that div, so that it's never visible on the website.我使用 CSS 来隐藏该 div,因此它永远不会在网站上可见。
<div id="total"><?php echo $total; ?></div>
#total {
display: none;
}
I use jQuery to find that div on the page, and get the text from it, which is the total number of relevant posts.我使用 jQuery 在页面上找到那个 div,并从中获取文本,这是相关帖子的总数。 I put that in a variable.我把它放在一个变量中。
total = $('#total').text();
I use jQuery to check whether the number of posts on the page is equal to the total number of posts, and, if it is, I hide the load more button.我使用 jQuery 来检查页面上的帖子数是否等于帖子总数,如果是,则隐藏加载更多按钮。 For my particular situation, I check in both the initial Ajax load and the Ajax call that fires when the load more button is clicked.对于我的特定情况,我检查了初始 Ajax 加载和单击加载更多按钮时触发的 Ajax 调用。
current = $('.item').length;
total = $('#total').text();
if ( current == total ) {
$('.load-more').css({
display: 'none'
});
}
I don't know if this is the best solution, or even a good solution, but it's what I've figured out to do, and it's working for me so far.我不知道这是否是最好的解决方案,甚至是一个好的解决方案,但这是我想出的办法,到目前为止它对我有用。
WP_Query has a parameter max_num_posts
which will tell you how many pages in total the query have. WP_Query 有一个参数max_num_posts
,它会告诉你查询总共有多少页。 Inside the callback function you can check if there any more pages left in a query and send the value in response.在回调函数中,您可以检查查询中是否还有更多页面并发送值作为响应。 Than you will be able to hide/show "load more" button with JS function based on that value.然后您将能够根据该值使用 JS 函数隐藏/显示“加载更多”按钮。 You can something similar to this:你可以做类似的事情:
function ajax_filter_get_posts( $category, $tag, $paged = 1 )
{
$paged = $_POST['paged']; // Pass a number of a page that you want to retrieve. Default is page #1
$category = $_POST['category'];
$tag = $_POST['tag'];
if($category)
{
$category_args = array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category
);
}
$args = array(
'posts_per_page' => 6,
'paged' => $paged, // pass required page number to WP_Query
'post_status' => 'publish',
'tag' => implode(",",$tag),
'tax_query' => array(
$category_args,
),
'category__in' => array(187,186,183,182,184),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$output = post_factory($post);
$result['response'][] = $output;
$result['status'] = 'success';
endwhile; else:
$result['response'] = '<h2>No posts found</h2>';
$result['status'] = '404';
endif;
// Check if there are any more pages to query and pass the boolean value in response
$result['more_pages'] = ( ( $query->max_num_pages - $paged ) > 0 ) ? true : false;
$result = json_encode($result);
echo $result;
die();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.