【问题标题】:Wordpress: Pagination of pages?Wordpress:页面分页?
【发布时间】:2020-02-22 11:44:08
【问题描述】:

在 wordpress 站点中,我有一个循环,可以在页面上显示某个类别的帖子。我有一个分页代码。显示分页。但是,如果我转到第 2、3、4 页……显示的结果始终是第一页。有人看到错误吗?

感谢您的帮助。我看不到错误。

我的代码:

<a class="xyz" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>

<?php 
if ( has_post_thumbnail() ) {
    the_post_thumbnail('medium');
} 
?>

<?php the_excerpt(); ?>
</div><!-- grid-item -->



<?php
      endwhile; 

       $total_pages = $tk_specials_querie->max_num_pages;
       if ($total_pages > 1){

        $current_page = max(1, get_query_var('page'));

         $paginate_links =  paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
             'show_all'        => False,
            //'end_size'        => 1,
            // 'mid_size'        => $pagerange,
             'prev_next'       => True,
             'type'         => 'plain',
             'end_size'     => 1,
             'mid_size'     => 2,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
             'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
        ));
    }  
    ?>

<?php
else :
  esc_html_e( 'Derzeit keine Beiträge!', 'text-domain' );
endif;
 wp_reset_postdata();
?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    通过这种方式,您可以在任何地方进行分页。您应该在(开始循环)和(结束循环)之间添加循环。

     <div class="posts-wrap">
        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            /* start the loop */
    
        $args = array(
            'post_type' => 'post', // Your post type name
            'posts_per_page' => 2,
            'paged' => $paged,
        );
        $loop = new WP_Query($args);
        if ($loop->have_posts()) {
            while ($loop->have_posts()) : $loop->the_post();
                ?>
    
    
           <div class="grid-item">
             <a class="xyz" href="<?php the_permalink(); ?>">
                <h3 class="entry-title"><?php the_title(); ?></h3>
                 <?php 
                if ( has_post_thumbnail() ) {
                    the_post_thumbnail('medium');
                } 
                ?>
                 <?php the_excerpt(); ?>
              </a><!-- grid-item -->
          </div>
    
          <?php
            endwhile;
    
         /* end the loop */   
    
          $total_pages = $loop->max_num_pages;
            if ($total_pages > 1) {
                ?>
                <div class="pagination">
                    <?php
                    $current_page = max(1, get_query_var('paged'));
    
                    echo paginate_links(array(
                        'base' => get_pagenum_link(1) . '%_%'.'/#posts-blog',
                        'format' => '/page/%#%',
                        'current' => $current_page,
                        'total' => $total_pages,
                        'prev_text' => __('<'),
                        'next_text' => __('>'),
                    ));
                    ?>
                </div>
                <?php
            }
        }
        wp_reset_postdata();
    
        if (function_exists("pagination")) {
            pagination($wp_query->max_num_pages);
        }
        ?>
    
        </div>
    

    【讨论】:

    • 对不起,不起作用。但我现在解决了。 (见上面的答案)
    【解决方案2】:

    好的,我现在做到了。如果有人想知道怎么做:

    
                 <div class="grid" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 445, "gutter": 20 }'>
    
    
    
    
                    <?php
                        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                        $query = new WP_Query( array(
                            'post_type'   => 'post',
                            'post_status' => 'publish',  
                            'posts_per_page'         => '50',
                            'paged'                 => $paged,
                            'order'                  => 'DESC',
                            'orderby'                => 'date',
                            'cat'                    => '949',
                        ) );
                    ?>
    
                    <?php if ( $query->have_posts() ) : ?>
    
                    <!-- begin loop -->
                    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
                       <div class="grid-item">
                            <a class="xyz" href="<?php the_permalink(); ?>"></a>
                            <h3 class="entry-title"><?php the_title(); ?></h3>
    
                            <?php 
                                if ( has_post_thumbnail() ) {
                                the_post_thumbnail('medium');
                                } 
                            ?>
                        </div><!-- grid-item -->
    
                    <?php endwhile; ?>
                    <!-- end loop -->
    
    
                    <div class="tk_pagination">
                        <?php 
                            echo paginate_links( array(
                                'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                                'total'        => $query->max_num_pages,
                                'current'      => max( 1, get_query_var( 'paged' ) ),
                                'format'       => '?paged=%#%',
                                'show_all'     => false,
                                'type'         => 'plain',
                                'end_size'     => 2,
                                'mid_size'     => 1,
                                'prev_next'    => true,
                                'prev_text'    => sprintf( '<i></i> %1$s', __( '<', 'text-domain' ) ),
                                'next_text'    => sprintf( '%1$s <i></i>', __( '>', 'text-domain' ) ),
                                'add_args'     => false,
                                'add_fragment' => '',
                            ) );
                        ?>
                    </div>
    
                        <?php wp_reset_postdata(); ?>
    
                        <?php else : ?>
                            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                        <?php endif; ?>
    
    
                    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 2012-10-08
      相关资源
      最近更新 更多