I am using the script below to list the custom taxonomies, but I still need a link to the parent of the current custom taxonomy for an "All" link.
Right now the links display as:
Shoes Shirt T-Shirt
Would like to include an "All" link so it will display as:
All Shoes Shirt T-Shirt
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$children = get_term_children( $term->term_id, $term->taxonomy );
echo get_category_link( get_queried_object_id() );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li='. $title.'&child_of=' . $term->term_id );
} else {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=' . $term->parent );
} ?>
I am using the script below to list the custom taxonomies, but I still need a link to the parent of the current custom taxonomy for an "All" link.
Right now the links display as:
Shoes Shirt T-Shirt
Would like to include an "All" link so it will display as:
All Shoes Shirt T-Shirt
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$children = get_term_children( $term->term_id, $term->taxonomy );
echo get_category_link( get_queried_object_id() );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li='. $title.'&child_of=' . $term->term_id );
} else {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=' . $term->parent );
} ?>
Share
Improve this question
asked Apr 4, 2019 at 18:04
Joe LandryJoe Landry
277 bronze badges
1 Answer
Reset to default 2Try this, which worked well for me:
$term = get_queried_object();
$children = get_term_children( $term->term_id, $term->taxonomy );
$has_children = ( ! is_wp_error( $children ) && ! empty( $children ) );
// Has children, or no parent.
if ( $has_children || ! $term->parent ) {
$parent =& $term; // reference to $term
// No children, but has parent.
} elseif ( $term->parent ) {
// Get the term object (or data like name) using get_term().
$parent = get_term( $term->parent );
}
echo '<h3><a href="' . esc_url( get_term_link( $parent ) ) . '">' . esc_html( $parent->name ) . '</a></h3>';
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=' . $parent->term_id );
Explanation/Notes:
On a taxonomy archive, you should use
get_queried_object()
to get the.. queried term — just like when you visit a single post page,get_queried_object()
would give you the queried post.You can use
get_term_link()
to get the term archive URL (e.g.http://example/category/uncategorized
).I intentionally didn't use the
title_li
parameter because you're only displaying level 1 children (i.e. you set thedepth
parameter to1
).
And I used the h3
tag, but you can of course change it to your liking. :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745625884a4636802.html
评论列表(0条)