0

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.

enter image description here

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

enter image description here

what I want to do

enter image description here

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 ...

enter image description here

enter image description here 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

1 Answer 1

0

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]' );

1
  • millions thanks I updated my post see my answer . you seem to know php well, I think maybe you should take a look at my posts I have been stuck on some details for a while.
    – neosan
    Jan 4, 2021 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.