문제

I have 4 categories for my custom post type. Once I go the first post of Category 1, I'd like this pagination, to only loop me through the posts within Category 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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top