【问题标题】:Wordpress WP_Query get next post with same categoryWordpress WP_Query 获得下一个相同类别的帖子
【发布时间】:2016-10-17 21:09:40
【问题描述】:

我正在尝试获取与 WP 中当前帖子具有相同类别的下一个帖子。我不是要获取下一篇文章 (next_post_link()) 的链接,而是要获取文章本身。

目前我只收到具有相同类别的最新帖子,而不是帖子本身。

$query = new WP_Query( array( 'category_name' => $maincat_slug, 'posts_per_page' => 1, 'post__not_in' => array( $post->ID )) );
if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post(); 
        get_template_part( 'template-parts/content', 'teaser' ); 
    endwhile;
endif;

$maincat_slug包括当前帖子的(第一个)类别标签(get_the_category())。

也许我们可以更改'post__not_in'以包含当前和所有以前的帖子?

编辑:

get_next_post_link 没有类别过滤器,所以我认为这在这里不起作用。

或者我们可以使用offset 在当前帖子之后开始。不知道如何计算循环内当前帖子的索引。

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    这就是我使用 wp_query offset 完成它的方式

    1. 第一次运行循环,检查循环中当前帖子的索引
    2. 将第二个循环的偏移量设置为当前页面的索引(+1)
    3. 使用与第一个循环的偏移量运行第二个循环。

    这样,第二个循环会忽略当前帖子之前的所有帖子,并显示当前帖子之后的第一个帖子。

    代码:

    // Get current category (first cat if multiple are set)
    $category = get_the_category(); 
    $maincat_slug = $category[0]->slug;
    
    // Get current Post ID
    $current_id = $post->ID; 
    
    // Reset offset
    $offset = 0;
    
    // Calculate offset
    $query = new WP_Query( array( 'category_name' => $maincat_slug ) );
    if ( $query->have_posts() ) : 
        while ( $query->have_posts() ) : 
            $query->the_post(); 
            $test_id = $post->ID;
            if ( $test_id == $current_id ) :
                // Set offset to current post
                $offset = $query->current_post + 1;
            endif;
        endwhile; 
    endif;
    
    // Display next post in category
    $query = new WP_Query( array( 'category_name' => $maincat_slug, 'posts_per_page' => 1, 'offset' => $offset) );
    if ( $query->have_posts() ) : 
        while ( $query->have_posts() ) : 
            $query->the_post(); 
            get_template_part( 'template-parts/content', 'teaser' ); 
        endwhile; 
    else :
        // Fallback 
    endif;
    

    【讨论】:

      【解决方案2】:

      您可以使用函数url_to_postid()从链接中检索id,然后获取帖子:

      $link = next_post_link();
      $postid = url_to_postid( $link );
      
      $query = new WP_Query( array( 'category_name' => $maincat_slug, 'posts_per_page' => 1, 'p' => $postid );
      if ( $query->have_posts() ) : 
          while ( $query->have_posts() ) : $query->the_post(); 
              get_template_part( 'template-parts/content', 'teaser' ); 
          endwhile;
      endif;
      

      【讨论】:

      • 不错的方法,我得到了与'post__not_in' => range(0,$post->ID)) 类似的结果。可悲的是,显示了错误的帖子(当前)。也许问题出在我的 Intuitive Custom Post Type Order 插件上?
      • 插件是问题的一部分。但仍然......这不起作用。首先它必须是get_next_post_link() 而不是next_post_link()。但这也行不通,因为:我们得到了下一篇文章的 ID。但是,如果下一篇文章没有 $maincut_slug 类,则查询不会有任何结果。对吗?
      • 正确,所以应该只针对 p=postID 进行查询
      • 是的,但是get_next_posts_link没有类别过滤器(不知道为什么!)。因此,无论类别如何,这都会获得下一个帖子链接。 next-post-link有一个类别过滤器......但这没有帮助。无论如何感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      相关资源
      最近更新 更多