[英]Get Prev/Next post link via post ID in WordPress
I am currently using the WordPress previous_post_link
and next_post_link
, which work as expected.我目前正在使用 WordPress
previous_post_link
和next_post_link
,它们按预期工作。 The issue is that I have imported tons of posts via CSV file, so now most posts have the same date/timestamp, and WP doesn't pick the right prev/next post as it is based on date by default I believe.问题是我已经通过 CSV 文件导入了大量帖子,所以现在大多数帖子都有相同的日期/时间戳,并且 WP 没有选择正确的上一个/下一个帖子,因为它默认基于日期我相信。 I have searched other answers which show how to search the ID, but I can't seem to implement it into a link, it only returns a plain ID.
我已经搜索了其他显示如何搜索 ID 的答案,但我似乎无法将它实现到链接中,它只返回一个纯 ID。 For sure I'm getting it wrong as I do not have much PHP knowledge.
我肯定弄错了,因为我没有太多 PHP 知识。
I am using this code:我正在使用这段代码:
<div class="prev"><?php previous_post_link( '%link', '%title', TRUE ); ?> </div>
<div class="next"><?php next_post_link( '%link', '%title', TRUE ); ?> </div>
I have found the code below but I don't know how to turn it into the code format above.我找到了下面的代码,但我不知道如何把它变成上面的代码格式。
<?php
function get_previous_post_id( $post_id ) {
global $post;
$oldGlobal = $post;
$post = get_post( $post_id );
$previous_post = get_previous_post();
$post = $oldGlobal;
if ( '' == $previous_post )
return 0;
return $previous_post->ID;
}
echo get_previous_post_id( $current_id );
An answer that can provide a working code is much appreciated.非常感谢可以提供工作代码的答案。 Thanks
谢谢
@CBroe pointed out a couple of filter hooks allowing a programmer to control the meaning of "next" and "previous". @CBroe 指出了几个过滤器钩子,允许程序员控制“下一个”和“上一个”的含义。
(WordPress customization code is all about hooks . If you don't know much about them, you would be wise to find out more.) (WordPress 自定义代码都是关于hooks的。如果您对它们了解不多,最好了解更多。)
The hooks he suggests, get_next_post_sort
and get_previous_post_sort
, give us php programmers a way to intervene in the database queries to look up next and previous posts.他建议的钩子
get_next_post_sort
和get_previous_post_sort
为我们 php 程序员提供了一种干预数据库查询以查找下一篇和上一篇文章的方法。
This code does it.这段代码做到了。 It tells the database to order by id, then post date.
它告诉数据库按 id 排序,然后发布日期。 The usual
$order
clause has this value "ORDER BY p.post_date $order LIMIT 1"
, and this filter adds p.ID
after the date.通常的
$order
子句具有此值"ORDER BY p.post_date $order LIMIT 1"
,并且此过滤器在日期后添加p.ID
Therefore posts with identical dates will be ordered by ID, which is what you say you need.因此,具有相同日期的帖子将按 ID 排序,这就是您所说的需要。
/** Filter to disambiguate identical-date post ordering.
*
* @param string $order_by Usually "ORDER BY p.post_date $order LIMIT 1"
* @param WP_Post $post The current post object.
* @param string $order Either 'ASC' or 'DESC'
*
* @returns string The modified $order_by string.
*/
function redibis_next_prev_by_id ( $order_by, $post, $order ) {
return "ORDER BY p.post_date $order, p.ID $order LIMIT 1";
}
add_filter ( 'get_next_post_sort', 'redibis_next_prev_by_id', 10, 3 );
add_filter ( 'get_prev_post_sort', 'redibis_next_prev_by_id', 10, 3 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.