php - WordPress 一页上多个查询错误

标签 php mysql wordpress

我在一页上有两个查询。第一个查询当前帖子,如果它是主类别的子类别,则显示这些子类别帖子。第二个查询当前类别 slug。错误是第二个查询显示第一个查询的类别。 wp_reset_query 不起作用吗?奇怪的是,这只发生在实时服务器上。我的本地工作正常。

   //First query
    foreach((get_the_category()) as $childcat) {
          if (cat_is_ancestor_of(26, $childcat)) {

              $subcat =  $childcat->cat_name;

    }}

    $sub_query = new WP_Query( array(
      'post_type' => 'work',
     'category_name' => $subcat,
      'posts_per_page' => 12,
      'orderby'=> 'date',
      'order'=> 'DESC',
      'paged'=> $paged,
       'post__not_in' => array( get_the_ID() )


    ) );

    if ( $sub_query->have_posts() ): ?>

    <?php while ( $sub_query->have_posts() ) : $sub_query->the_post();  ?>
     // content
    <?php endwhile; ?>
    <?php endif; ?>
   <?php wp_reset_query(); ?> 

    //second query
       if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0];
  } else {
    $cat = get_category( get_query_var( 'cat' ) );
  }
  $cat_slug = $cat->slug;

    $work_query = new WP_Query( array(
      'post_type' => 'work',
     'category_name' => $cat_slug,
      'posts_per_page' => 12,
      'orderby'=> 'date',
      'order'=> 'DESC',
      'paged'=> $paged,
     'post__not_in' => array( get_the_ID() )


    ) );

  if ( $work_query->have_posts() ): ?>

    <?php while ( $work_query->have_posts() ) : $work_query->the_post(); ?>
   // content
    <?php endwhile; ?>
    <?php endif; ?>
   <?php wp_reset_query(); ?> 

最佳答案

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( '分页' ) : 1; 在 wp_query 之前使用它

引用此链接 https://codex.wordpress.org/Pagination

关于php - WordPress 一页上多个查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37556478/

相关文章:

php - 函数不搜索特定单词

php - PHP 函数没有正确返回值?

php - WooCommerce:将端点分配给我的帐户页面中的自定义模板

MySQL - 根据另一个列值搜索列值

php - 使用 WordPress Functions.php 文件将 ACF 字段作为简码返回

php - 录音php和jQuery

javascript - 如何使用 AJAX JSON 将多个变量从 Javascript 传递到 PHP?

php - 登录时它没有带我到正确的位置

关于更新当前时间戳的 Mysql 5.6 列在 hibernate 中不起作用

mysql - 在 MySQL 中进行透视时如何生成没有零值字段的单行结果集