具有自定义类别/分类的分页自定义帖子类型

时间:2014-04-17 19:40:21

标签: php pagination custom-post-type taxonomy

我的自定义帖子类型有4个类别。一旦我去了第1类的第一篇文章,我就喜欢这个分页,只能让我通过第1类中的帖子。

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

我的自定义帖子类型被称为"工作"我的自定义分类法被称为" work"。

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

<?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>';
} ?>

有没有其他方法可以做到这一点,只会让我通过该类别中的帖子?

1 个答案:

答案 0 :(得分: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>';
} ?>
相关问题