WordPress分类列表 - 分页


Wordpress Taxonomy listing - Pagination

我发现这个出色的功能可以显示特定自定义分类法下列出的所有帖子。效果很好。在这里找到 http://gilbert.pellegrom.me/wordpress-list-posts-by-taxonomy 我尝试了许多想法,但并不成功,试图对返回的数据进行分页。我要么没有得到任何数据,要么它继续显示整个列表。我的一些分类法有超过 10K 个相关的帖子。因此,分页似乎是合乎逻辑的。

我想做的是; 让返回的信息创建"n"个帖子的页面,并为其他页面(1,2,...4,5等)。任何帮助将不胜感激。

我把它扔在我的函数文件中;

   function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(),
   $wp_query_args = array() ){
   $tax_terms = get_terms( $taxonomy, $get_terms_args );
    if( $tax_terms ){
    foreach( $tax_terms  as $tax_term ){
        $query_args = array(
            'post_type' => $post_type,
            "$taxonomy" => $tax_term->slug,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'ignore_sticky_posts' => true
        );
        $query_args = wp_parse_args( $wp_query_args, $query_args );
        $my_query = new WP_Query( $query_args );
        if( $my_query->have_posts() ) { ?>
            <h2 id="<?php echo $tax_term->slug; ?>" class="title">
            <?php echo $tax_term->name; ?></h2>
            <ul>
            <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
               <li><a href="<?php the_permalink() ?>" rel="bookmark" 
                    title="Permanent Link to <?php the_title_attribute(); ?>">
                    <?php the_title(); ?></a></li>
            <?php endwhile; ?>
            </ul>
            <?php
        }
        wp_reset_query();
    }
}
}
?>

这段代码进入模板,填充任何"分类"名称,它会显示数据。我不确定的另一个问题,分页是否应该进入函数或模板。

<div class="my_class">
<?php
list_posts_by_taxonomy( 'my_posttype', 'taxo_mytaxo' );
?>
</div>

谢谢大家!

为了首先对查询进行分页,您必须使用 paged 参数。
要获取一些额外的信息,请查看分页的 wordpress codex

通常你会得到这样的分页变量:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

然后,通过将它包含在代码的查询参数中来将其传递给查询:

$query_args = array(
            'post_type'           => $post_type,
            "$taxonomy"           => $tax_term->slug,
            'post_status'         => 'publish',
            'posts_per_page'      => -1,
            'ignore_sticky_posts' => true
            'paged'               => $paged //I've added it here
        );

然后你必须构建分页链接,如下所示(这将在循环内完成):

<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>