Php 分页具有自定义类别/分类的自定义文章类型

Php 分页具有自定义类别/分类的自定义文章类型,php,pagination,custom-post-type,taxonomy,Php,Pagination,Custom Post Type,Taxonomy,我的自定义帖子类型有4个类别。一旦我读到第一类的第一篇文章,我希望这个分页,只让我在第一类的文章中循环 我随后创建了我的分页,但它只有在我输入了一个分类术语的名称时才起作用,然后它才适用于该类别(即剧院) 我的自定义帖子类型称为“works”,我的自定义分类法称为“work” 以下是我目前的代码: <?php // get_posts in same custom taxonomy $postlist_args = array( 'posts_per_page' => -1, 'o

我的自定义帖子类型有4个类别。一旦我读到第一类的第一篇文章,我希望这个分页,只让我在第一类的文章中循环

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

我的自定义帖子类型称为“works”,我的自定义分类法称为“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>';
} ?>


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

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

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