简体   繁体   English

从存档中调用的CPT页面上的WordPress上一个和下一个链接

[英]Wordpress previous and next links on CPT Page called from Archive

I have a CPT and 3 custom taxonomies. 我有一个CPT和3个自定义分类法。 For each of the taxonomies there exist archive pages with items for the taxonomy term ordered by title. 对于每个分类法,都有存档页面,其中按标题对分类法术语的项目进行排序。

The item page has prevoius/next-Links in the footer, but these are linked to the previous/next item of this CPT in publish date. 项目页面的页脚中有prevoius / next-links,但这些链接在发布日期已链接到本CPT的上一个/下一个项目。

How can I substitute this with a link to the previous/next item in the archive, as the visitor would expect? 如访问者所期望的那样,如何用指向归档文件中上一个/下一个项目的链接替换它?

A first idea is to transport the loop content from the archive page and looking up the neighbor items? 第一个想法是从存档页面传输循环内容并查找相邻项?

You need to add this code to your single-myposttype. 您需要将此代码添加到您的单一myposttype中。

/* Fist get the posts from specific taxonomy */
$postByTaxonomy = get_posts(array(
  'post_type' => 'post', // fruit
  'numberposts' => -1,
  //'order'          => 'ASC', // you can add to order by name
  //'orderby'        => 'title', // you can add to order by name
  'tax_query' => array(
    array(
      'taxonomy' => 'fruit-category', // category of Fruit CPT
      'field' => 'slug',
      'terms' => 'fruit-category-1', // This is the one you check from editor page
    )
  )
)); 

/* Store the IDs in array from get_posts above */
$theIDs = array();
foreach ($postByTaxonomy as $pbt) {
  $theIDs[] += $pbt->ID;
}
/* print_r($theIDs) = Array ( [0] => 3696 [1] => 3697 [2] => 128 [3] => 4515 [4] => 4516 [5] => 4514 ) */


/* Now we will use a php function array_search() for us to get the current post and we can set the previous and next IDs */
$current = array_search( get_the_ID(), $theIDs); /* here you need to get the id of the post get_the_ID() or $post->ID */
$prevID = $theIDs[$current-1];
$nextID = $theIDs[$current+1];

/* DISPLAY - Now that we have the IDs of the prev/next post you can use them to your template in your case the permalink */
<a href="<?php echo get_permalink($prevID); ?>"> Previous</a>
<a href="<?php echo get_permalink($nextID); ?>"> Next</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ACF 关系 上一页 下一页 CPT 链接 - ACF Relationship Previous Next links of CPT Wordpress ACF:使用页面(具有特定页面模板)作为CPT存档页面 - Wordpress ACF: Using a page (with a specific page template) as a CPT archive page Wordpress - CPT 存档页面中的两个循环,从主查询中排除帖子 - Wordpress - two loops in CPT archive page, exclude posts from main query 为什么我的CPT在单个帖子视图中不显示下一个和上一个链接? - Why are my CPT not showing next and previous links on single post view? Wordpress 下一页/上一页链接在第二页时不起作用 - Wordpress next/previous page links not working when at the second page WordPress - 如何将锚点添加到上一页/下一页链接 - WordPress - How to add anchor to previous/next page links Wordpress中损坏的类别/标签上一个条目/下一页链接 - Broken Category/Tag Previous Entries/Next page links in Wordpress 从Wordpress获取下一个/上一个“照片页面” - Getting the next/previous 'photo page' from wordpress Wordpress输出链接到自定义查询的上一个和下一个帖子 - Wordpress output links to previous and next posts from custom query 如何仅从Wordpress CPT删除所有Feed链接? - How to Remove all Feed Links from Wordpress CPT Only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM