我有一个CPT和3个自定义分类法。对于每个分类法,都有存档页面,其中按标题对分类法术语的项目进行排序。
项目页面的页脚中有prevoius / next-links,但这些链接在发布日期已链接到本CPT的上一个/下一个项目。
如何像访问者期望的那样用链接到档案中上一个/下一个项目的链接替换它?
第一个想法是从存档页面传输循环内容并查找相邻项目?
答案 0 :(得分:0)
您需要将此代码添加到您的单一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>