所以我使用ajax过滤并将帖子加载到容器中。我想一次将帖子数量限制为6个,如果帖子超过6个,可以在下面添加更多加载按钮,但我不想添加页面,因为我在同一页面上有几个容器我正在使用同样的处理方式,而我的理解是页面会在网址中添加/ page-1或类似内容(我错了吗?)。
无论哪种方式,我只想知道如何检查是否有更多符合此条件的帖子,以便我可以显示加载更多按钮,然后当加载更多按钮触发时,我如何只加载6个。我必须在某处保留页面变量吗?还是有另一种更聪明的方式。
这是我的查询
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();
}
答案 0 :(得分:1)
我知道这篇文章大约有一年的历史,但是如果其他人需要答案,这就是我正在实施的解决方案。我正在使用Ajax从PHP文件中引入wp_query。该文件的输出替换了我页面上div的内容。这只是相关的代码,而不是我的完整代码。
点击“加载更多”按钮,我使用jQuery计算页面上的帖子数量,并将该计数放入变量中。帖子分配了一个独特的课程。
current = $('.item').length;
我使用Ajax将发布计数发送到PHP文件。
$.get(functions_home.url, { offset: current }, function(data) {
$('.grid').html(data);
}
我将jQuery的帖子数量提取到PHP文件中的变量中,然后使用该变量在$ args中设置wp_query“offset”。
$offset = $_GET['offset'];
$args = array (
'offset' => $offset
);
查询($ query)运行后,我使用$ query-&gt; found-posts告诉我相关帖子的总数,并将其放在变量中。
$total = $query->found_posts;
我在我的PHP文件中创建一个div并使用找到的posts变量,以便填充相关帖子的总数。我使用CSS来隐藏该div,因此它在网站上永远不可见。
<div id="total"><?php echo $total; ?></div>
#total {
display: none;
}
我使用jQuery在页面上找到div,并从中获取文本,这是相关帖子的总数。我把它放在变量中。
total = $('#total').text();
我使用jQuery来检查页面上的帖子数量是否等于帖子的总数,如果是,我隐藏了更多的加载按钮。对于我的特殊情况,我检查了初始Ajax加载和单击加载更多按钮时触发的Ajax调用。
current = $('.item').length;
total = $('#total').text();
if ( current == total ) {
$('.load-more').css({
display: 'none'
});
}
我不知道这是最好的解决方案,还是一个很好的解决方案,但这是我想要做的事情,到目前为止它对我有用。
答案 1 :(得分:1)
WP_Query具有参数max_num_posts
,它将告诉您查询总共有多少页。
在回调函数中,您可以检查查询中是否还有更多页面,并发送该值作为响应。然后,您将可以基于该值使用JS函数隐藏/显示“加载更多”按钮。您可以执行以下操作:
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();
}