WordPress improve shortcode with more parameters

when you are a new designer and start working with several content editors in wordpress, the first thing you realize is that it is not easy to find the functions you want exactly. plugin editors can't think of everything.. and especially extract information to display them everywhere in the desired and consistent manner.

that's why wordpress shortcodes exist. but you have to master them.. first! (which is precisely my handicap currently)

Grab your cup of coffee ☕ and admire the detailed visual of today's issue

It will be very short and precise,

before starting look at what taxonomies we have in our post..

enter image description here

my current code allows me to do figure 1, as you can see it quickly becomes limited when you try to be .. more creative

add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
    // Don't extract, rather parse shortcode params with defaults.
    $atts = shortcode_atts( array(
        'link' => 'true',
        'taxonomy' => 'property_city'
    ), $atts, 'parent-child' );

    // Check the $atts parameters and their typecasting.
    //var_dump( $atts );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );

    /* You can pass conditions here to override
     * $link based on certain conditions. If it's
     * a single post, current user is editor, etc.

    foreach( $terms as $term ){
        if( $term->parent != 0 ){
            $parent_term = get_term( $term->parent, $taxonomy );
            if ($atts['link'] !== 'false') {
                printf( '<a href="%s">%s</a>, ', esc_url( get_term_link($parent_term) ), $parent_term->name );
            } else {
                echo $parent_term->name . ', ';

        if ($atts['link'] !== 'false') {
            printf( '<a href="%s">%s</a>', esc_url( get_term_link($term) ), $term->name );
        } else {
            echo $term->name;

    return ob_get_clean();

enter image description here in this figure 1 we don't need to display "residential" and "rental" it doesn't make sense it's confusing. we don't have control over this.

but..What if I want to test and accomplish figure 2, figure 3 and figure 4

enter image description here

You : but there are already shortcodes what is it???

yes, don't worry, I took care to simulate a logic of things inside shortcodes, maybe that's not exactly how it's going to be. or how things work ..

I prefer to remain cautious and wait for your opinion or solution.

if logic allows, we need to separate the parent and childs values ​​and paste them back as we want like a puzzle

Something Like 👀

↑[taxonomy-parent = "name_of_my_taxonomy"]

↓[taxonomy-child = "name_of_my_taxonomy"]

or simply reverse in some cases

←[child-parent] (manhattan, new york)

→[parend-child] (new york, manhattan)

note that I used [acf field "name field here"] to invoke a price value, if it is possible to fit it inside our combination or leave it like that?

final word: you can tell I spent some time working out and thinking about this, if only coding was that easy for me I would have done even better. for now that's all I need more flexibility to display information in different ways.

Read more here: https://stackoverflow.com/questions/65727060/wordpress-improve-shortcode-with-more-parameters

Content Attribution

This content was originally published by neosan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: