好吧,这可能有点过于复杂了,但至少它应该可以工作(未经测试)。
只需这样调用函数
get_adjacent_without_sticky( get_the_ID(), true); // get previous post
get_adjacent_without_sticky( get_the_ID(), false); // get next post
如果
get_previous_post()
允许ID传递,而不仅仅在当前帖子的上下文中运行它。现在您可以复制
get_adjacent_post()
并根据您的需要进行调整,但我不喜欢在WordPress中直接使用SQL,因此使用常见函数时,它应该是这样工作的:
function get_adjacent_without_sticky($id, $previous = true) {
// get date of current post as reference
$date = explode(\'.\', get_the_date(\'d.m.Y\', $id));
$date_array = array(
\'day\' => $date[0],
\'month\' => $date[1],
\'year\' => $date[2],
);
// for previous post we need the first post before current one
if ($previous) {
$order = \'DESC\';
$date_query = array(
\'before\' => $date_array,
);
}
// for next post we need the first post after current one
else {
$order = \'ASC\';
$date_query = array(
\'after\' => $date_array,
);
}
$args = array(
// order sticky posts as regular ones
\'ignore_sticky_posts\' => 1,
// get only the one post we want
\'posts_per_page\' => 1,
\'order\' => $order,
\'orderby\' => \'date\',
\'date_query\' => $date_query,
);
// now run that query
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
$the_query->the_post();
$result = get_post( get_the_ID() );
} else {
$result = NULL;
}
wp_reset_postdata(); // Restore original Post Data
return $result;
}
相关主题:
(我的答案基本上是将这两个源组合成一个函数。)