最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Infinite looping next post link within a certain category on a post

programmeradmin3浏览0评论

I'm looking to create an infinitely looping 'next post' link at the bottom of a post that only links to posts in that category.

So if I have three posts in the same category – 1, 2 and 3 – it should work as follows:

  • clicking the link on post 1 should take me to post 2
  • clicking the link on post 2 should take me to post 3
  • clicking the link on post 3 should take me to post 1

I've got code to loop through all posts in this way, but am stuck on how to edit it so that it only loops through posts with the same category as the current post.

if( get_adjacent_post(true, '', false) ) { 
next_post_link($format = '%link', $link = 'Next Article', $in_same_term = true);
} else { 
$thiscat = get_the_category();
$last = new WP_Query(array('query' => 'posts_per_page=1&order=ASC',
'category_name' => $thiscat)); $last->the_post();
    echo '<a href="' . get_permalink() . '">Next Article</a>';
wp_reset_query();
};

I'm looking to create an infinitely looping 'next post' link at the bottom of a post that only links to posts in that category.

So if I have three posts in the same category – 1, 2 and 3 – it should work as follows:

  • clicking the link on post 1 should take me to post 2
  • clicking the link on post 2 should take me to post 3
  • clicking the link on post 3 should take me to post 1

I've got code to loop through all posts in this way, but am stuck on how to edit it so that it only loops through posts with the same category as the current post.

if( get_adjacent_post(true, '', false) ) { 
next_post_link($format = '%link', $link = 'Next Article', $in_same_term = true);
} else { 
$thiscat = get_the_category();
$last = new WP_Query(array('query' => 'posts_per_page=1&order=ASC',
'category_name' => $thiscat)); $last->the_post();
    echo '<a href="' . get_permalink() . '">Next Article</a>';
wp_reset_query();
};
Share Improve this question edited Apr 13, 2020 at 18:55 zackwww asked Apr 13, 2020 at 16:44 zackwwwzackwww 32 bronze badges 6
  • have you read about the $in_same_term parameter in developer.wordpress/reference/functions/next_post_link and using the categories' parameters in WP_Query developer.wordpress/reference/classes/wp_query/… and how to get a post's category developer.wordpress/reference/functions/get_the_category ? – Michael Commented Apr 13, 2020 at 18:12
  • @Michael Thanks a lot for this guidance. Have edited my code in the question but it still doesn't quite seem to be working. what am I getting wrong? – zackwww Commented Apr 13, 2020 at 18:58
  • get_the_category() returns an array, even if you only have one coategory in the post; use only the first element: – Michael Commented Apr 13, 2020 at 19:23
  • @michael ah yes i'd just got to that myself - still seems to be a dud though! if( get_adjacent_post(true, '', false, 'category') ) { next_post_link($format = '%link', $link = 'Next Article', $in_same_term = true, $taxonomy = 'category'); } else { $thiscat = get_the_category(); $catslug = $thiscat[0]->slug; $last = new WP_Query(array('query' => 'posts_per_page=1&order=ASC', 'category_name' => $catslug)); $last->the_post(); echo '<a href="' . get_permalink() . '">Next Article</a>'; wp_reset_query(); }; – zackwww Commented Apr 13, 2020 at 19:24
  • your query parameters are a bit mixed; try: WP_Query( array( 'posts_per_page' => 1, 'order' => ASC, 'category_name' => $catslug ) ) – Michael Commented Apr 13, 2020 at 19:31
 |  Show 1 more comment

1 Answer 1

Reset to default 0

based on the docu:

  • the $in_same_term parameter in https://developer.wordpress/reference/functions/next_post_link/
  • the categories' parameters in WP_Query https://developer.wordpress/reference/classes/wp_query/#category-parameters
  • and how to get a post's category https://developer.wordpress/reference/functions/get_the_category/
  • and in collaboration with @zackwww;

'infinitely looping 'next post' link at the bottom of a post that only links to posts in that category'

if( get_adjacent_post(true, '', false, 'category') ) {
   next_post_link($format = '%link', $link = 'Next Article', $in_same_term = true, $taxonomy = 'category'); 
} else { 
   $thiscat = get_the_category(); 
   $catslug = $thiscat[0]->slug; 
   $last = new WP_Query( array( 
      'posts_per_page' => 1, 
      'order' => 'ASC', 
      'category_name' => $catslug  
    ) ); 
   $last->the_post(); 
   echo '<a href="' . get_permalink() . '">Next Article</a>'; 
   wp_reset_postdata(); 
}
发布评论

评论列表(0)

  1. 暂无评论