WordPress Shorcode Display hierarchical taxonomy [child, parent]

I have a slight problem to display the parents of taxonomy in wordpress.
(CPT=property and tax = property_city)

I did a little research to figure out what to do.

but I can’t quite figure out how to display them.

I will give you an illustration, you will quickly understand.

Wordpress Shorcode Display hierarchical taxonomy [child, parent]

what I have (not actual shortcode or any code! Just dynamic tag term showing taxonomy without parent)

Wordpress Shorcode Display hierarchical taxonomy [child, parent]

what I want to do

Wordpress Shorcode Display hierarchical taxonomy [child, parent]

the ideal will be to display them in shortcode, so I add them where I need.

I tested this method but it obviously does not work..

I think I still did not understand something..

function taxonomy_hierarchy() {
    global $post;
    $taxonomy = 'property_city'; //Put your custom taxonomy term here
    $terms = wp_get_post_terms( $post->ID, $taxonomy );
    foreach ( $terms as $term )
        {
    if ($term->parent == 0) // this gets the parent of the current post taxonomy
    {$myparent = $term;}
        }
    echo ''.$myparent->name.'';
    // Right, the parent is set, now let's get the children
    foreach ( $terms as $term ) {
        if ($term->parent != 0) // this ignores the parent of the current post taxonomy
        { 
        $child_term = $term; // this gets the children of the current post taxonomy
        echo ''.$child_term->name.'';
        }
    }   
}

add_shortcode( 'child-parent', function () {

    $content = "echo ''.$child_term->name.'' echo ''.$myparent->name.''";

    return $content;
} );

code found here : link

UPDATE 04/01/2021

millions thank to Stef
I did the tests on my loop and single post it works well.

function taxonomy_hierarchy() {
global $post;
$post_id = $post->ID;
$return = '';
$terms = wp_get_post_terms( $post->ID, 'property_city' ); //Put your custom taxonomy term here
foreach ( $terms as $term ) {
    // this gets the parent of the current post taxonomy
    
    
     //attempt to make terms clickable
    $term_link = get_term_link( $term );
    
    if ($term->parent != 0) {
        $return .= $term->name. ', ' . get_term( $term->parent, 'property_city' )->name;
    } else {
        $return .= $term->name;
    }
}
return $return;
}
add_shortcode( 'city-area', 'taxonomy_hierarchy' );

and finally understood how to do shortcodes and the logic behind it.
as i try to understand the logic behind the php, I forgot to mention to make the terms clickable get_term_link I should be able to enable and disable them (parent or child or both) with “//” (optionnal)
How to do that ?

I already have this piece of code which redirects them to my custom request (inside elementor builder for exemple ) since shortcode is not clickable it does not work on them..

add_filter ('term_link', function ($ termlink, $ term, $ taxonomy) {
// taxonomy city
if ('property_city' == $ taxonomy) {
         $ termlink = trailingslashit (get_home_url ()). 'property /?_city ='. $ term-> slug;
     }
     return $ termlink;
}, 10, 3);

After a little thought, what if someday i need something like this …

Wordpress Shorcode Display hierarchical taxonomy [child, parent]

Wordpress Shorcode Display hierarchical taxonomy [child, parent]
how to make it flexible and display the available terms hierarchically?
I think this
WordPress: Get taxonomy hierarchy, including children
has a link with my question?

but I can’t put the puzzle together

thank you for your patience

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your function taxonomy_hierarchy() is not actually linked to the shortcode. Your function taxonomy_hierarchy() is also not a proper shortcode function.
You can find how to create proper shortcodes here.

A shortcode function also has to return, don’t use echo.

This is how your function taxonomy_hierarchy should kinda look. I changed the code for your needs:

function taxonomy_hierarchy() {

global $post;

$return = '';

$terms = wp_get_post_terms( $post->ID, 'property_city' );

foreach ( $terms as $term ) {

    // this gets the parent of the current post taxonomy
    if ( $term->parent != 0 ) {

        $return .= $term->name ', ' . get_term( $term->parent, 'property_city' )->name;

    } else {

        $return .= $term->name;

    }

}

return $return;

}

Then add this to create the shortcode from the function taxonomy_hierarchy().

add_shortcode( 'child-parent', 'taxonomy_hierarchy' );

When using the shortcode frontend, you can then use [child-parent]. Backend you can use echo do_shortcode( '[child-parent]' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x