Hi, I have created archive page which works well. I set it up to show the current name of the category [wpv-taxonomy-archive info= "name" ], but I don´t know how to show it´s parent category.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Dave,
This can be achieved by using the info='parent' attribute.
Thanks,
Shane
Hi Shane,
[wpv-taxonomy-archive info='parent'] does not work for me. It just shows a number... instead of parent category. For information Taxonomy type is set as Hierarchical.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Dave,
It seems that the parameter returns the id of the parent. Since we have the id we can use some custom code to get the parent title.
Add the following to your functions.php file
// Add Shortcode
function term_parent( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'parent_id' => '',
'taxonomy' => '',
),
$atts
);
$term = get_term( $atts['parent_id'], $atts['taxonomy'] );
return $term->name;
}
add_shortcode( 'term_parent', 'term_parent' );
Then you can use this shortcode by doing this.
[term_parent parent_id="[wpv-taxonomy-archive info='parent']" taxonomy="taxonomy_slug"]
Now replace the word 'taxonomy_slug' with the slug of your taxonomy and it should give you the title of your parent term.
Please let me know if this helps.
Thanks,
Shane
Brilliant, works great!
But one thing to have it 100% - I need to get a URL of this parent category. I tried to modify your shortcode, but without success.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Dave,
Try this one instead.
// Add Shortcode
function term_parent( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'parent_id' => '',
'taxonomy' => '',
),
$atts
);
$term = get_term( $atts['parent_id'], $atts['taxonomy'] );
$term_url = get_term_url($term);
return '<a href="' . esc_url( $term_url) . '">' . $term->name . '</a>';
}
add_shortcode( 'term_parent', 'term_parent' );
Try this and let me know if it helps.
Thanks,
Shane
Hi Shane,
I tried this code instead of previous, but there must be some error, because if I place the code into functions.php the page would not load.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Dave,
I see the issue 🙂
Use this function as i've fixed it.
// Add Shortcode
function term_parent( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'parent_id' => '',
'taxonomy' => '',
),
$atts
);
$term = get_term( $atts['parent_id'], $atts['taxonomy'] );
$term_url = get_term_link($term);
return '<a href="' . esc_url( $term_url) . '">' . $term->name . '</a>';
}
add_shortcode( 'term_parent', 'term_parent' );
Thanks,
Shane
Hi Shane,
thanks, we are getting close to solution, it works now in child archive pages. But if I want to see parent page, there is the same problem as before, the page would not load. Any idea?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Dave,
Would you mind providing me with admin access to the website so that I can have a look as it should work.
The private fields have been enabled for your next response.
Thanks,
Shane
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Dave,
I'm not sure where you placed the code I gave but if you remove it does the archive show up ?
Please let me know.
Thanks,
Shane
Hi Shane,
you can find shortcode in layout link I send you in private message in Loop Output Editor - [term_parent parent_id="[wpv-taxonomy-archive info='parent']" taxonomy="kategorie"]
And the shortcode code is in functions.php, toolset-starter theme, not the child one.
Yes, if I remove it, the parent category will show up.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Dave,
I'm making a final adjustment to the code.
// Add Shortcode
function term_parent( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'parent_id' => '',
'taxonomy' => '',
),
$atts
);
if ($atts['parent_id'] != 0){
$term = get_term( $atts['parent_id'], $atts['taxonomy'] );
$term_url = get_term_link($term);
return '<a href="' . esc_url( $term_url) . '">' . $term->name . '</a>';
}
}
add_shortcode( 'term_parent', 'term_parent' );
I've changed it to account for if the archive doesn't have a parent. I've already tested the code on your site so its working now 🙂
Thanks,
Shane
Great work, Shane. Thank you.