繁体   English   中英

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

[英]How to echo parent name and permalink of custom taxonomy category on subcategory archive page

我有一个用于自定义帖子类型 (case_studies) 的自定义分类法 (expertise_taxonomy) 和一个存档模板 (taxonomy-expertise.php)。 我的问题是,在子类别页面上,我需要在页面顶部回显其父类别名称及其永久链接(想想面包屑)。 我能够获得父类别 ID,但无法获得名称永久链接。 看起来get_ancestors是正确的方向,但我得到的只是一个空数组。

这是我的模板代码的相关部分:

    <?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>

一个示例所需的输出是在查看“Apple”子类别存档页面时,它的父类别“Fruit”在面包屑中,如下所示:

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

任何帮助将不胜感激!

您正在寻找的功能是get_term_link 它使用术语对象,ID或子词段和分类名称,然后将URL返回到术语登录页面。

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>

Deepti让我开始工作,其余的工作如下:

$parent = get_queried_object()->parent; // gets parent category ID
$taxonomy = get_query_var('taxonomy'); // gets the taxonomy name
$term_link = get_term_link( $parent, $taxonomy ) // Deepti's answer to get parent category link
$r = $term_link; // declares new variable with parent category link
$r = explode('/', $r); // separates url parts based on '/' delimiter
$r = array_filter($r); // creates array of parts
$r = array_merge($r, array()); // resets array keys
$code = $r[3]; // variable containing the third array key - works for my purposes because I will always need the third part (last part) of my url   
$string = str_replace("-", " ", $code); // removes the dash if dash exists in my slug and replaces it with a space

<?php echo $string; ?>

这为我的面包屑提供了父类别名称(Fruit):专业知识| 水果 苹果

我的博客中有很多类别,我希望子类别的存档网址为:

mydomain.com/child-category/

代替

mydomain.com/parent-category/child-category/

我怎样才能获得这个风险?

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM