2014-04-17 119 views
0

我有4類我的自定義帖子類型。一旦我去了第一類的第一個職位,我想這個分頁,只能通過類別1中的帖子循環我。分頁自定義帖子類型與自定義類別/分類

我跟着this article來創建我的分頁,但它只有在我輸入名稱我的分類術語之一 - 然後它只適用於該類別(即劇院)

我的自定義帖子類型被稱爲「作品」,我的自定義分類稱爲「工作」。

這裏是我到目前爲止的代碼:

<?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

我找到了解決這個問題的辦法。以下是我正在使用的最終腳本 - 以防其他人一直在尋找此答案:

<?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>'; 
} ?> 
相關問題