【问题标题】:Wordpress Taxonomy listing - PaginationWordpress 分类列表 - 分页
【发布时间】:2013-10-17 02:43:09
【问题描述】:

我发现这个出色的功能可以显示在特定自定义分类下列出的所有帖子。它工作得很好。在这里找到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>

谢谢大家!

【问题讨论】:

    标签: php wordpress function templates pagination


    【解决方案1】:

    为了对您的查询进行分页,首先您必须使用paged 参数。
    要获得更多信息,请查看Pagination的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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2015-02-20
      • 1970-01-01
      相关资源
      最近更新 更多