【问题标题】:Pagination Custom Post Type with Custom Category/Taxonomy具有自定义类别/分类的分页自定义帖子类型
【发布时间】:2014-06-02 05:58:09
【问题描述】:

我的自定义帖子类型有 4 个类别。一旦我进入第 1 类的第一篇文章,我想要这个分页,让我只循环浏览第 1 类中的文章。

我按照this article 创建了我的分页,但它只有在我输入我的一个分类术语的名称时才有效 - 然后它只适用于该类别(即剧院)

我的自定义帖子类型称为“作品”,我的自定义分类称为“作品”。

到目前为止,这是我的代码:

<?php // get_posts in same custom taxonomy
$postlist_args = array(
'posts_per_page'  => -1,
'orderby'         => 'menu_order title',
'order'           => 'ASC',
'post_type'       => 'works',
'work' => 'theatre'
); 
$postlist = get_posts( $postlist_args );

// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}

// get and echo previous and next post in the same taxonomy        
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if ( !empty($previd) ) {
echo '<a class="button-icon" rel="prev" href="' . get_permalink($previd). '">Previous</a>';
}
if ( !empty($nextid) ) {
echo '<a class="button-icon" rel="next" href="' . get_permalink($nextid). '">Next</a>';
} ?>

还有其他方法可以做到这一点,只会让我循环浏览该类别中的帖子吗?

【问题讨论】:

    标签: php pagination custom-post-type taxonomy


    【解决方案1】:

    我找到了解决这个问题的方法。这是我正在使用的最终脚本——以防其他人也一直在寻找这个答案:

    <?php // get_posts in same custom taxonomy
    
    $posttype = get_query_var(post_type);
    $taxonomies=get_taxonomies(
    array(
    object_type => array ($posttype)
    ),
    'names'
    );
    foreach ($taxonomies as $taxonomy ) { //Assigning all tax names of the posttype to an array
    $taxnames[] = $taxonomy;
    }
    
    $terms = get_the_terms( $post->ID, $taxnames[0] );
    foreach ( $terms as $term ) { //Assigning tax terms of current post to an array
    $taxterms[] = $term->name;
    }
    
    $postlist_args = array(
    'posts_per_page' => -1,
    'orderby' => 'date',
    'order' => 'DSC',
    'post_type' => $posttype,
    'tax_query' => array(
    array(
    'taxonomy' => $taxnames[0],
    'field' => 'name',
    'terms' => $taxterms
    )
    )
    );
    $postlist = get_posts( $postlist_args );
    
    // get ids of posts retrieved from get_posts
    $ids = array();
    foreach ($postlist as $thepost) {
    $ids[] = $thepost->ID;
    }
    
    // get and echo previous and next post in the same taxonomy        
    $thisindex = array_search($post->ID, $ids);
    $previd = $ids[$thisindex-1];
    $nextid = $ids[$thisindex+1];
    if ( !empty($previd) ) {
    echo '<a class="button-icon" rel="prev" href="' . get_permalink($previd). '">Previous</a>';
    }
    if ( !empty($nextid) ) {
      echo '<a class="button-icon" rel="next" href="' . get_permalink($nextid). '">Next</a>';
    } ?>
    

    【讨论】:

      猜你喜欢
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      相关资源
      最近更新 更多