【问题标题】:Get Next and Previous posts link through a custom query in WordPress通过 WordPress 中的自定义查询获取下一篇和上一篇文章链接
【发布时间】:2016-11-27 17:33:39
【问题描述】:

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

$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 来获取指向我的单个文章页面上的下一篇和上一篇文章的链接。有什么想法吗?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    previous_post_link()next_post_link(),正如文档所说,需要在循环中。但是单曲后呢?您打开一篇文章,即使您使用全局查询对象,它也不会有与您的文章列表相同的查询数据 - 给您带来奇怪和/或循环的结果。

    对于仍在寻求答案的任何人,我创建了一个简单的函数 get_adjacent_posts()(不要将它与 get_adjacent_post() 原生 wordpress 函数混淆),它总是会得到先前和下一篇文章,无论查询和函数的位置如何。

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

    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']);
    

    警告:这个功能很耗费资源,所以如果你有很多帖子,请不要使用它。它从提供的查询中加载并迭代所有帖子以查找下一个和上一个帖子(如您在其代码中所见)。

    有一个更好的方法可以做到这一点,即直接调用数据库,但无论如何我都懒得做,而且我在超过 100 个帖子上从来不需要这个代码。

    希望你觉得它有用!

    【讨论】:

      【解决方案2】:

      您可以使用函数get_adjacent_post(),该函数可用于检索下一个和上一个帖子对象。第三个参数必须设置为 true 才能检索上一篇文章,而 false 必须设置为下一篇文章。最后一个参数允许在选择和受限的分类中获得相邻的帖子。

      $previous_adjacent_post = get_adjacent_post(true,'',true, 'product_cat');
      $next_adjacent_post = get_adjacent_post(true,'',false, 'product_cat');
      
      if(is_a($previous_adjacent_post, 'WP_Post')){
           $previous_link = get_permalink($previous_adjacent_post->ID);
           $previous_title = $previous_adjacent_post->post_title;
      }
      if(is_a($next_adjacent_post, 'WP_Post')){
           $next_link = get_permalink($next_adjacent_post->ID);
           $next_title = $next_adjacent_post->post_title;
      }
      

      在此示例中,is_a() 条件在未找到响应时避免错误(尝试从字符串中获取对象,因为响应为空或 null)。

      更多细节和例子get_adjacent_post()

      您最终可以使用过滤器"get_{$adjacent}_post_where": 来按自定义字段进行过滤。

      编辑:

      通过您添加的链接和说明,您似乎可以使用您使用的插件来做到这一点:

      in_same_meta

      指示下一个/上一个帖子是否必须具有相同的 自定义字段值作为当前帖子。您必须传递姓名 要匹配的自定义字段。例如,如果您有一个自定义字段 命名为“发布者”,并且您希望下一个/上一个链接指向 来自同一出版商的书名:

      <?php next_post_link_plus( array('in_same_meta' => 'publisher') ); ?>
      

      请注意 in_same_meta 与自定义字段不兼容 排序。如果 order_by 设置为“custom”或“numeric”, in_same_meta 被禁用。

      希望对你有帮助!

      【讨论】:

      • 恐怕这对我一点帮助都没有。我不得不使用这个插件来获得想要的结果。 ambrosite.com/plugins/…
      • 感谢您的努力,但该代码也不适用于我。这就是给我正确链接的原因: 'numeric', 'meta_key' => 'date_of_auction') ); ?>
      • 我不明白你是想使用原生 WordPress 功能还是只使用插件。如果你想用wordpress函数重现插件行为,这是可能的,如何,这是另一个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2018-07-22
      • 1970-01-01
      • 2019-07-12
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多