分类页面中的WordPress分页不起作用

vngu2lb8  于 2022-11-02  发布在  WordPress
关注(0)|答案(1)|浏览(236)

我想在WordPress主题的类别页面上显示分页
这是我的密码

global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
    $posts = get_posts(array(
        'numberposts' => 150, 
        'posts_per_page'=>16,
        'offset' => 0, 
        'category__not_inint' => array($category), 
        'post_status'=>'publish', 
        'order'=>'ASC' 
    ));

  foreach($posts as $post){
        setup_postdata($post);

        $city_name = get_field( "city-name" );

        $display = '<a href="' . get_permalink() . '">' . $city_name . '</a>';
    }

    the_posts_pagination(); 
    wp_reset_query(); 
    return $display;

并显示分页。但所有页面的结果都与第一页类似。

smdnsysy

smdnsysy1#

如果你需要对查询进行分页,你不应该使用get_posts。虽然可以这样做,但这是一个很难实现的目标。相反,你应该查看WP_Query
有关WP_Query -WP_Query @ wordpress.org的进一步阅读
您的代码可能如下所示;

<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1; // explain to wordpress we need this paged
$wp_query = new WP_Query(array( // the query
'post_type' => 'post',
'post_category' => '',
'post_status' => 'publish',
'numberposts' => 150,
'posts_per_page' => 15,
//'orderby' => 'title', 
'order' => 'ASC',
'paged' => $paged)); 

while ($wp_query->have_posts()) : $wp_query->the_post(); // the loop

// some code to make it look pretty
?>
<div class="post-grid">
<a href="<?php the_permalink(); ?>">
<h3 class="card-title"><?php the_title(); ?> </h3>
</a>
</div>

<?php endwhile;

echo ( paginate_links($args = array(
'base'         => site_url().'%_%', // site_url prefix is needed for pagination on homepage
'format'       => '?page=%#%',
'total'        => $wp_query->max_num_pages,
'current'      => $paged,
'show_all'     => false,
'end_size'     => 2,
'mid_size'     => 2,
'prev_next'    => true,
'prev_text'    => 'Prev',
'next_text'    => 'Next',
'type'         => 'list',
'add_args'     => false,
'add_fragment' => '' 
 )));

wp_reset_query();

相关问题