php 分页WordPress存档自定义帖子

az31mfrm  于 2022-12-21  发布在  PHP
关注(0)|答案(1)|浏览(159)

我有一个自定义的文章类型在WordPress和存档页面上,我想添加分页。我有以下代码,但当我点击第2页或“下一页”,它重新加载第1页。
我的第一个想法是它应该被添加到wp_reset_postdata()之前,但是这会在每个帖子旁边显示链接。
我错过了什么?

<!-- custom query for village life posts -->
<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
   'posts_per_page' => 6,
   'post_type' => 'villagelife',
   'orderby' => 'post_date',
   'order' => 'ASC',
   'paged' => $paged,
);
$villagelifeArchive = new WP_Query($args);

  while($villagelifeArchive->have_posts()) {
    $villagelifeArchive->the_post(); 
    ?> 
        <div class="grid-item">
            <div class="grid-item-wrapper">
                <div class="grid-item-container">
                <?php 
                
                if ( has_post_thumbnail() ) : 
                ?>  
                  <div class="grid-image-top">
                    <span class="centered project-image-bg"><a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('archive-village-life-image'); ?></a>
                    </span>
                  </div>
                <?php
                endif;
                ?>  
                  <div class="grid-item-content">
                    <span class="item-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
                    <span class="item-category"><?php echo get_the_term_list($post->ID, 'village-life-category', '', ', ', ' '); ?></span>
                    <span class="item-excerpt"><?php echo wp_trim_words(get_the_content(), 18);?></span>
                   
                    <?php echo '<a class="button ast-button more-info" href='.get_the_permalink() . ">Read More..</a>";  ?>
                  </div>
                </div>                
            </div>           
        </div> 
        <?php
         } wp_reset_postdata(); 

         $total_pages = $villagelifeArchive->max_num_pages;
         print_r($total_pages);  //give me 2 ..the correct answer.

         if ($total_pages > 1){

            $current_page = max(1, get_query_var('paged'));
            ?> <div> <?php
            echo paginate_links(array(
                  'base' => get_pagenum_link(1) . '%_%',
                  'format' => '/page/%#%',
                  'current' => $current_page,
                  'total' => $total_pages,
            ));
            ?> </div> <?php
         }    
        
        ?>
fcg9iug3

fcg9iug31#

我目前无法帮助您的问题,但我已经得到了我用于分页的php。
将以下内容添加到functions.php文件中

<?php
function og_pagination() {
    global $wp_query;

    $big = 999999999; 

    $links = paginate_links( array(
        'base' => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
        'current' => max( 1, get_query_var( 'paged' ) ),
        'total' => $wp_query->max_num_pages,
        'mid_size' => 5,
        'prev_next' => true,
        'prev_text' => '
        <svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="14px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M9.7,18.3L9.7,18.3c0.39-0.39,0.39-1.02,0-1.41L5.83,13H21c0.55,0,1-0.45,1-1v0c0-0.55-0.45-1-1-1H5.83l3.88-3.88 c0.39-0.39,0.39-1.02,0-1.41l0,0c-0.39-0.39-1.02-0.39-1.41,0L2.7,11.3c-0.39,0.39-0.39,1.02,0,1.41l5.59,5.59 C8.68,18.68,9.32,18.68,9.7,18.3z"/></svg>',
        'next_text' => '
        <svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="14px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M14.29,5.71L14.29,5.71c-0.39,0.39-0.39,1.02,0,1.41L18.17,11H3c-0.55,0-1,0.45-1,1v0c0,0.55,0.45,1,1,1h15.18l-3.88,3.88 c-0.39,0.39-0.39,1.02,0,1.41l0,0c0.39,0.39,1.02,0.39,1.41,0l5.59-5.59c0.39-0.39,0.39-1.02,0-1.41L15.7,5.71 C15.32,5.32,14.68,5.32,14.29,5.71z"/></svg>',
        'type' => 'array',
    ));

    if ( $links ) {
        echo '<div class="pagination">';
        echo '<ul>';

        foreach ( $links as $link ) {
            printf( '<li>%s</li>', str_replace( '/page/1', '', $link ) );
        }

        echo '</ul>';
        echo '</div>';
    }
}

?>

相关问题