通过 WordPress 中的自定义查询获取下一个和上一个帖子链接 [英] Get Next and Previous posts link through a custom query in WordPress

查看:28
本文介绍了通过 WordPress 中的自定义查询获取下一个和上一个帖子链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过自定义查询获取文章帖子页面 (single.php) 上的下一个和上一个帖子链接.我曾尝试使用 previous_post_link()next_post_link() 函数,但它们通过 ID 获取帖子.我的索引页上有以下循环查询:

I'm trying to fetch the next and previous posts link on an article post page (single.php) through a custom query. I have tried using the previous_post_link() and next_post_link() functions but they fetch posts by ID. I have the following loop query on my index page:

$args = array(
  'post_type' => 'auction_dates',
  'paged' => $paged, 
  'posts_per_page' => 1, 
  'meta_key'    => 'date_of_auction', 
  'orderby' => 'meta_value_num', 
  'order' => 'ASC');

如您所知,帖子按自定义字段date_of_auction"而非 ID 排序.我希望使用该自定义字段而不是 ID 获取指向我的单个文章页面上的下一篇和上一篇文章的链接.有什么想法吗?

As you can tell, the posts are ordered by a custom field 'date_of_auction', not IDs. I want the link to the next and previous posts on my single article page to be fetched using that custom field instead of IDs. Any ideas?

推荐答案

previous_post_link()next_post_link(),正如文档所说,需要在循环中.但是单身后呢?您打开一个帖子,即使您使用全局查询对象,它也不会具有与您的帖子列表相同的查询数据 - 为您提供奇怪和/或循环的结果.

previous_post_link() and next_post_link(), as the docs say, need to be within the loop. But what about post single? You open one post and even if you use the global query object it won't have the same query data as your post listing - giving you wierd and/or looping results.

对于仍在寻求答案的任何人,我创建了一个简单的函数 get_adjacent_posts()(不要将它与 get_adjacent_post() 原生 wordpress 函数),无论查询和函数的位置如何,它都将始终获取上一个和下一个帖子.

For anyone still seeking an answer to this, I've created a simple function get_adjacent_posts() (don't get it confused with get_adjacent_post() native wordpress function) that will always get previous and next posts regardless of the query and the placement of the function.

您需要做的就是提供查询 args 数组作为参数,它将返回一个包含上一个和下一个 WP 帖子对象的数组.

All you need to do is provide query args array as a parameter and it will return an array with previous and next WP post objects.

function get_adjacent_posts($args) {
    global $post;

    $all_posts = get_posts($args);
    $len = count($all_posts);
    $np = null;
    $cp = $post;
    $pp = null;

    if ($len > 1) {
        for ($i=0; $i < $len; $i++) {
            if ($all_posts[$i]->ID === $cp->ID) {
                if (array_key_exists($i-1, $all_posts)) {
                    $pp = $all_posts[$i-1];
                } else {
                    $new_key = $len-1;
                    $pp = $all_posts[$new_key];

                    while ($pp->ID === $cp->ID) {
                        $new_key -= 1;
                        $pp = $all_posts[$new_key];
                    }
                }

                if (array_key_exists($i+1, $all_posts)) {
                    $np = $all_posts[$i+1];
                } else {
                    $new_key = 0;
                    $np = $all_posts[$new_key];

                    while ($pp->ID === $cp->ID) {
                        $new_key += 1;
                        $np = $all_posts[$new_key];
                    }
                }

                break;
            }
        }
    }

    return array('next' => $np, 'prev' => $pp);
}

示例用法:

$args = array(
    'post_type' => 'custom_post_type',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'orderby' => 'title'
);

$adjacent = get_adjacent_posts($args);

$next_title = $adjacent['next']->post_title;
$next_image = get_the_post_thumbnail_url($adjacent['next']->ID, 'square');
$next_url = get_permalink($adjacent['next']);

$prev_title = $adjacent['prev']->post_title;
$prev_image = get_the_post_thumbnail_url($adjacent['next']->ID, 'square');
$prev_url = get_permalink($adjacent['prev']);

警告:此功能会占用大量资源,因此如果您有很多帖子,请不要使用它.它加载并迭代提供的查询中的所有帖子,以查找下一个和上一个帖子(如您在代码中所见).

WARNING: This function is resource expensive, so do not use it if you have a lot of posts. It loads and iterates all of the posts from the provided query to find next and previous posts (as you can see in it's code).

有一种更好的方法可以做到这一点,那就是直接调用数据库,但无论如何我都懒得这样做,而且我从来没有在 100 多个帖子中使用过这段代码.

There is a better way to do this, which is directly make calls to the database, but I'm way too lazy for that anyways, plus I never needed this code on more than 100 posts.

希望你觉得它有用!

这篇关于通过 WordPress 中的自定义查询获取下一个和上一个帖子链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆