wordpress - 从存档调用的 CPT 页面上的 Wordpress 上一个和下一个链接
问题描述
我有一个 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>
推荐阅读
- python - Qt:停靠小部件如何获得其初始大小?
- vba - 如何模拟箭头键被按下并按住然后释放?VBA
- swift - 无法保存 EKEvent,因为日历缺少默认组织者
- pandas - 我的 .loc 在应该工作的地方出错:“无法从重复的轴重新索引”
- electron - 如何使用 Mocha 测试需要 Node API 的自定义模块?“无法读取未定义的属性‘要求’”
- macros - 如何让这个公式只填写空白单元格?
- akka - 带有临时队列的 Alpakka JMS 请求/响应 - 开箱即用?
- android - 使用“仅 GPS”模式或“Wifi 网络”时,FusedLocationProviderClient 不起作用
- django - 使用 SQL 数据库和 NoSQL 数据库
- sapui5 - 如何在 SAP UI5/Fiori 中创建 shell 插件应用程序?