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!
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>
Deepti got me started and here's how I did the rest:
$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; ?>
And that gives me the parent category name (Fruit) for my breadcrumb: Expertise | Fruit | Apple