dongzhuanlei0768 2018-03-08 00:40
浏览 51

如何在子类别归档页面上回显自定义分类法类别的父名称和永久链接

I have a custom taxonomy (expertise_taxonomy) for a custom post type (case_studies) and an archive template for it (taxonomy-expertise.php). My problem is that on a subcategory page I need to echo its parent category name at the top of the page and its permalink (think breadcrumb). I'm able to get the parent category ID but can't get the name permalink. It looked as though get_ancestors was the right direction but all I get is an empty array.

Here's the pertinent part of my template code:

    <?php

     $taxonomy = get_query_var('taxonomy');
     $termId = get_queried_object()->term_id;
     $title = get_field('expertise_deliverables_title', $taxonomy . '_' . $termId);
     $content = get_field('expertise_deliverables', $taxonomy . '_' . $termId);
     $parent = get_queried_object()->parent; 
     ?>

    <?php if ( have_posts() ) : ?>

    <p><a href="/expertise">Expertise</a> |

     <!-- This is where the parent permalink should go -->
     <a href="/">

     <!-- This is where the parent ID is echoed instead of the name -->
     <?php echo $parent; ?>

     </a> | <?php echo str_replace('Expertise: ','', get_the_archive_title()); ?></p>

An example desired output would be while looking at the 'Apple' child category archive page it would have its parent category 'Fruit' in the breadcrumb like so:

Expertise | <a href="/fruit">Fruit</a> | Apple

Any help would really be appreciated!

  • 写回答

2条回答 默认 最新

  • dongzongzhi6953 2018-03-08 01:28
    关注

    The function you are looking for is get_term_link. It takes either a term object, ID or slug and a taxonomy name and returns a URL to the term landing page.

    Check more info about this function from Wordpress Codex:

     $taxonomy = get_query_var('taxonomy');
     $termId = get_queried_object()->term_id;
     $title = get_field('expertise_deliverables_title', $taxonomy . '_' . $termId);
     $content = get_field('expertise_deliverables', $taxonomy . '_' . $termId);
     $parent = get_queried_object()->parent; 
     ?>
    <?php $term_link = get_term_link( $parent, $taxonomy );?>
    
    <?php if ( have_posts() ) : ?>
    
    <p><a href="/expertise">Expertise</a> |
    
     <!-- This is where the parent permalink should go -->
     <a href="<?php echo $term_link; ?>">
    
     <!-- This is where the parent ID is echoed instead of the name -->
     <?php echo $parent; ?>
    
     </a> | <?php echo str_replace('Expertise: ','', get_the_archive_title()); ?></p>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)