【问题标题】:pagination doesn't work on a single page of WordPress分页不适用于 WordPress 的单个页面
【发布时间】:2019-08-16 21:38:18
【问题描述】:

分页不适用于 WP 的单页。

在下面的代码中,除了分页在下一页显示相同的内容之外,一切看起来都很好。

我尝试了在互联网上找到的所有可能的解决方案,但都没有奏效。我对 WP 和 php 比较陌生,所以如果你能查明可能是错误的代码,那将非常有帮助。

          <?php
          $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' 
         ) : 1;
          $args = array(
            'posts_per_page' => 3,
            'paged'          => $paged,
            'post_type' => 'book',
            'offset'   => 1,

            'tax_query' => array( 
              array(
                'taxonomy' => 'news_cat', 
                'field' => 'slug', 
                'terms' => array( 'disney' ) 
              ),
            ),   
          );?>

          <?php $the_query = new WP_Query( $args ); 
          ?>


        <?php if ($the_query->have_posts()) ?>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>  

        <a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >                                                         
        <li>
          <div>
            <figure><img src="<?php the_field('thumb'); ?>" alt="">           
          </figure>
          </div>
          <span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
          <p><?php the_title(); ?></p>
        </li>
        </a>

    <?php endwhile; ?>


    <?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
    $args = array (
        'prev_text' => '',
        'next_text' => '',
        'show_all'  => false,
        'mid_size'  => 1,
        'type'      => 'list'
    );
    the_posts_pagination($args);?>   

我希望分页能够正常工作,而不是在所有页面上显示相同的内容。

如您所见,我想让分页仅在带有“迪士尼”蛞蝓的帖子上工作。

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    您可以替换代码而不是旧代码

    <?php
              $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' 
             ) : 1;
              $args = array(
                'posts_per_page' => 3,
                'paged'          => $paged,
                'post_type' => 'book',
                'offset'   => 1            
                ),   
              );?>
    
              <?php $the_query = new WP_Query( $args ); 
              ?>
    
    
            <?php if ($the_query->have_posts()) ?>
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>  
    
            <a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >                                                         
            <li>
              <div>
                <figure><img src="<?php the_field('thumb'); ?>" alt="">           
              </figure>
              </div>
              <span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
              <p><?php the_title(); ?></p>
            </li>
            </a>
    
        <?php endwhile; ?>
    
    
        <?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
        $args = array (
            'prev_text' => '',
            'next_text' => '',
            'show_all'  => false,
            'mid_size'  => 1,
            'type'      => 'list'
        );
        the_posts_pagination($args);?>   

    现在,您可以检查分页是否正常工作。

    【讨论】:

    • 感谢您的评论,但分页仍然不起作用。它在所有页面上显示相同的内容。另外,我只想要显示“迪士尼”蛞蝓的帖子。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    相关资源
    最近更新 更多