简体   繁体   English

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

[英]Pagination Custom Post Type with Custom Category/Taxonomy

I have 4 categories for my custom post type. 我的自定义帖子类型有4个类别。 Once I go the first post of Category 1, I'd like this pagination, to only loop me through the posts within Category 1. 一旦我转到类别1的第一篇帖子,我希望这种分页方式只能让我浏览类别1中的帖子。

I followed this article to create my pagination, but it only works if I enter the name of one of my taxonomy terms – and then it only works for that category (ie. Theatre) 我遵循了本文来创建分页,但是只有输入了我的一个分类法术语的名称时,该分页才有效–然后,该分类仅适用于该类别(即剧院)

My Custom Post Type is called "works" and my custom taxonomy is called "work". 我的自定义帖子类型称为“作品”,而我的自定义分类法称为“作品”。

Here's my code so far: 到目前为止,这是我的代码:

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

Is there another way to do this, that will only loop me through the posts within that category? 还有另一种方法可以使我循环浏览该类别中的帖子吗?

I found a solution to this problem. 我找到了解决此问题的方法。 Here's the final script I'm using – in case anyone else has been looking for this answer as well: 这是我使用的最终脚本,以防其他人也一直在寻找此答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM