最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shortcode - Wordpress Shorcode Display hierarchical taxonomy [child, parent]

programmeradmin0浏览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.

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

what I want to do

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

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

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.

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

what I want to do

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

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

Share Improve this question edited Jan 4, 2021 at 23:35 neosan asked Jan 2, 2021 at 23:25 neosanneosan 1175 bronze badges
Add a comment  | 

1 Answer 1

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

发布评论

评论列表(0)

  1. 暂无评论