在wp中实现分页;不起作用


Implementing pagination in wp doesn't work

我正试图在wordpress主题中添加一些代码,以便在帖子底部显示分页。这是我的分页循环:

<main id="main">
            <?php 
            // the query
            $args = array('posts_per_page' => 2 );
            $the_query = new WP_Query( $args ); 
            ?>
            <?php if ( $the_query->have_posts() ) { ?>
                <!-- loop -->
                <?php while ( $the_query->have_posts() ) {
                            $the_query->the_post(); ?>
       <article id="post"> 
                    <div id="thumbnail">
                        <?php
                        if ( has_post_thumbnail() ) {
                             the_post_thumbnail(); } ?>
                </div>
               <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
               <div class="entry">
                    <?php the_excerpt(); ?>
               </div>

       </article>

            <?php } } else { ?>
            <p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
            <?php }  ?>
            <!-- pagination -->
                <?php
    if($the_query->max_num_pages>1){?>
        <p class="paged">
        <?php
          if ($paged > 1) { ?>
            <a href="<?php echo '?paged=' . ($paged -1); //prev link ?>"><</a>
                            <?php }
        for($i=1;$i<=$the_query->max_num_pages;$i++){?>
            <a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
            <?php
        }
        if($paged < $the_query->max_num_pages){?>
            <a href="<?php echo '?paged=' . ($paged + 1); //next link ?>">></a>
        <?php } ?>
        </p>
    <?php } ?>
<!--  end pagination -->    

           <!-- end of the loop -->

           <?php wp_reset_postdata(); ?>

    </main>

当我浏览源代码时,我找不到分页。我做错了什么?找不到答案,没有适合我的代码。如果有人能帮助我,那就太好了。:)

使用默认的WordPress分页,这里有一个例子:

<?php
// set the "paged" parameter 
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'posts_per_page' => 5,
    'paged' => $paged,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// the loop
   while ( $the_query->have_posts() ) : $the_query->the_post(); 
      the_title();
   endwhile;
// Pagination
echo get_next_posts_link( 'Older', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer' );
// clean up after our query
wp_reset_postdata(); 
else:
_e( 'Sorry, no posts matched your criteria.' );
endif;

要自动显示下一个和上一个链接,只需使用以下功能:

next_posts_link();
previous_posts_link();

https://codex.wordpress.org/Pagination可以帮上忙。在自定义循环中,对于不受限制的页面,将$max_pages变量作为零传递会有所帮助,因此它看起来像:

next_posts_link("Older Posts", 0);

如果您希望分页包含页面链接,格式为<1 2 3>您可以使用WordPress函数paginat_links,而不仅仅是下一个/上一个链接。

因此,对于您的示例,代码如下所示:

<?php 
//query
$paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max( 1, get_query_var('paged') ));
$args = array(
    'posts_per_page' => 2,
    'paged' => $paged,
);
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()) {
    while ($the_query->have_posts()) {
        $the_query->the_post();
        //display your post here
    }  
    //pagination
    if($the_query->max_num_pages > 1){ ?> 
        <div class="paged">
            <?php 
            $big = 999999999; // need an unlikely integer
            echo paginate_links( array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '?paged=%#%',
                'current' => $paged,
                'total' => $the_query->max_num_pages,
                'prev_text' => __('« Previous'),
                'next_text' => __('Next »'),
            ) ); 
            ?> 
        </div> 
    <?php }
} else {
    echo '<p>'._e( 'Die Posts entsprechen nicht den Kriterien.' ).'</p>';   
}
wp_reset_postdata();
?>