我为自定义帖子类型(case_studies)提供了自定义分类(expertise_taxonomy),并为其提供了归档模板(taxonomy-expertise.php)。我的问题是,在子类别页面上,我需要在页面顶部回显其父类别名称及其永久链接(想想breadcrumb)。我能够获得父类别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
真的很感激任何帮助!
答案 0 :(得分:1)
您正在寻找的功能是get_term_link
。它需要术语对象,ID或slug和分类名称,并返回术语登录页面的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>
答案 1 :(得分:0)
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): 专业知识|水果|苹果