php - 如何与下一个转到其他帖子,但保持在同一类别中。我使用投资组合插件 wordpress
问题描述
这是我尝试过的,但它不起作用,它没有显示任何内容
<?php
next_post_link( '%link', '%title', TRUE );
previous_post_link( '%link', '%title', TRUE );
?>
这有效,但不按类别排序
<?php
next_post_link();
previous_post_link();
?>
我使用摄影主题 wordpress:https ://themeforest.net/item/photography-responsive-photography-theme/13304399
解决方案
global $post;
$post_id = $post->ID; // current post ID
$cat = get_the_category();
$current_cat_id = $cat[0]->cat_ID; // current category ID
$args = array(
'category' => $current_cat_id,
'orderby' => 'post_date',
'order' => 'DESC'
);
$posts = get_posts( $args );
// get IDs of posts retrieved from get_posts
$ids = array();
foreach ( $posts as $thepost ) {
$ids[] = $thepost->ID;
}
// get and echo previous and next post in the same category
$thisindex = array_search( $post_id, $ids );
$previd = isset( $ids[ $thisindex - 1 ] ) ? $ids[ $thisindex - 1 ] : 0;
$nextid = isset( $ids[ $thisindex + 1 ] ) ? $ids[ $thisindex + 1 ] : 0;
if ( $previd ) {
?><a rel="prev" href="<?php echo get_permalink($previd) ?>">Previous</a><?php
}
if ( $nextid ) {
?><a rel="next" href="<?php echo get_permalink($nextid) ?>">Next</a><?php
}
推荐阅读
- c# - .NET Core 5 Blazor 服务器 - 无法要求用户进行身份验证 - 重定向过多
- ios - 如何在 SwiftUI 中使用 ObservableObject 向其他类发送值
- javascript - css 文件中的警报属性
- azure - Azure MS Team 与 Azure DevOps 任务板的集成仅显示问题,我的工作项在哪里?
- terminal - 黑客练习/如何将新的终端命令传递给现有的终端命令?
- .net - 哪个 IIS 站点或应用程序池导致事件查看器中的 .NET 运行时错误?
- r - 具有二元结果的行的平均值?
- python - sns.kdeplot() 中出现意外的参数错误
- 3d - 在一定距离处制作平行多边形
- python - 当页面上有这么多特定元素时,如何选择该特定元素?Selenium webdriver python;