[英]Wordpress previous and next links on CPT Page called from Archive
我有一個CPT和3個自定義分類法。 對於每個分類法,都有存檔頁面,其中按標題對分類法術語的項目進行排序。
項目頁面的頁腳中有prevoius / next-links,但這些鏈接在發布日期已鏈接到本CPT的上一個/下一個項目。
如訪問者所期望的那樣,如何用指向歸檔文件中上一個/下一個項目的鏈接替換它?
第一個想法是從存檔頁面傳輸循環內容並查找相鄰項?
您需要將此代碼添加到您的單一myposttype中。
/* Fist get the posts from specific taxonomy */
$postByTaxonomy = get_posts(array(
'post_type' => 'post', // fruit
'numberposts' => -1,
//'order' => 'ASC', // you can add to order by name
//'orderby' => 'title', // you can add to order by name
'tax_query' => array(
array(
'taxonomy' => 'fruit-category', // category of Fruit CPT
'field' => 'slug',
'terms' => 'fruit-category-1', // This is the one you check from editor page
)
)
));
/* Store the IDs in array from get_posts above */
$theIDs = array();
foreach ($postByTaxonomy as $pbt) {
$theIDs[] += $pbt->ID;
}
/* print_r($theIDs) = Array ( [0] => 3696 [1] => 3697 [2] => 128 [3] => 4515 [4] => 4516 [5] => 4514 ) */
/* Now we will use a php function array_search() for us to get the current post and we can set the previous and next IDs */
$current = array_search( get_the_ID(), $theIDs); /* here you need to get the id of the post get_the_ID() or $post->ID */
$prevID = $theIDs[$current-1];
$nextID = $theIDs[$current+1];
/* DISPLAY - Now that we have the IDs of the prev/next post you can use them to your template in your case the permalink */
<a href="<?php echo get_permalink($prevID); ?>"> Previous</a>
<a href="<?php echo get_permalink($nextID); ?>"> Next</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.