簡體   English   中英

具有自定義類別/分類的分頁自定義帖子類型

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

我的自定義帖子類型有4個類別。 一旦我轉到類別1的第一篇帖子,我希望這種分頁方式只能讓我瀏覽類別1中的帖子。

我遵循了本文來創建分頁,但是只有輸入了我的一個分類法術語的名稱時,該分頁才有效–然后,該分類僅適用於該類別(即劇院)

我的自定義帖子類型稱為“作品”,而我的自定義分類法稱為“作品”。

到目前為止,這是我的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM