繁体   English   中英

如何检查 wordpress 查询是否有更多帖子?

[英]How do i check if a wordpress query has more posts?

所以我使用 ajax 过滤帖子并将其加载到容器中。 我想一次将帖子数量限制为 6 个,如果帖子超过 6 个,则在下方添加一个加载更多按钮,但我不想添加页面,因为我在同一页面上有几个容器我使用相同的处理方式,我的理解是页面会在 url 中添加一个 /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();
}

我知道这篇文章已经发布了大约一年,但如果其他人需要答案,这是我正在实施的解决方案。 我正在使用 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 = $_GET['offset'];

$args = array (
    'offset' => $offset
);

查询 ($query) 运行后,我使用 $query->found-posts 告诉我相关帖子的总数,并将其放入一个变量中。

$total = $query->found_posts;

我在我的 PHP 文件中创建一个 div 并使用找到的帖子变量,以便用相关帖子的总数填充它。 我使用 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'
    });

}

我不知道这是否是最好的解决方案,甚至是一个好的解决方案,但这是我想出的办法,到目前为止它对我有用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM