1

I work with several front-end editors only. in these cases where the editors are very different and are not flexible! I use code snippets to display a piece of information I need in a consistent way everywhere.

it's so simple that I feel like no one understands what I'm doing or trying to do. please read carefully and look at the illustrations.

In my case today

I have a taxonomy called "property_city" attached to the CTP "property" (nothing extraordinary)

enter image description here

its interests me because I want to display its terms in this way [Parent] -> [Child of parent] -> [Child of parent] -> etc all in hierarchy way

let's try to post an ad ok?

my apartment is located in manhattan so I selectd manhattan. by default the parent New York are not displayed.

with this snippet code it's possible

(/!\ don't be confused, we only use snippet codes no php files or templates to modify. we just inject a snippet./!)

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
    if ($term->parent != 0) {
        $return .= $term->name. ', ' .get_term( $term->parent, 'property_city' )->name;
    } else {
        $return .= $term->name;
    }
}
return $return;
}
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );

Done! New York,Manhattan now displayed.

My question is how to make this shortcode [parent-child] MORE flexible? with just output a text (=nolink) parameter or output links (=link).

in our example it will look like that

[parent-child=nolink] for my loops for e.g.

enter image description here

[parent-child=link] for the posts.

enter image description here

thanks if you have any idea how to do it

1 Answer 1

3

Take a gander at the add_shortcode() documentation and you'll see that the callback function is passed three parameters. The most important (and relevant to this) is the first $atts parameter.

I would do something like this:

add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
    $atts = shortcode_atts( array(
        'link' => true,
        'taxonomy' => 'property_city'
    ), $atts, 'parent-child' );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $taxonomy );

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

    ob_start();
    foreach( $terms as $term ){
        if( $term->parent != 0 ){
            $parent_term = get_term( $term->parent, $taxonomy );
            echo (filter_var($atts['link'], FILTER_VALIDATE_BOOLEAN)) ? sprintf( '<a href="%s">%s</a>, ', esc_url( get_term_link($parent_term) ), $parent_term->name ) : "{$parent_term->name}, " ;
        }

        echo (filter_var($atts['link'], FILTER_VALIDATE_BOOLEAN)) ? sprintf( '<a href="%s">%s</a>', esc_url( get_term_link($term) ), $term->name ) : $term->name ;
    }   

    return ob_get_clean();
}

This, using the shortcode_atts() function allows you to set some default parameters for your shortcode. I've also set it so taxonomy can be overwritten as well, which makes it much more extensible (for use later, in other projects, etc.)

I've changed the code slightly as well to use Output Buffering since it's a bit faster and cleaner when dealing with Ternary Comparisons and outputs like this compared to string concatenation (imo).

What it does is check to see if the $link attribute has been passed before determining to output the linked name, or just the name in plain text, and echoes the result into the output buffer.

This will allow you to get the following results:

[parent-child]
  • <a href="#">New York</a>
  • <a href="#">New York</a>, <a href="#">Manhattan</a>

[parent-child link="true"]
  • <a href="#">New York</a>
  • <a href="#">New York</a>, <a href="#">Manhattan</a>

[parent-child link="false"]
  • New York
  • New York, Manhattan

[parent-child link="false" taxonomy="some_other_taxonomy"]
  • Top Level Term
  • Top Level Term, Child Level Term

And so on. As I alluded to in the PHP comment, you can override the $link boolean at any time before the foreach loop, based on whatever conditions you want as well. So you can make the $link always return true if is_single() is true, or always return false if the current user isn't an editor, or anything else you can think of.

Documentation & Function Reference:

Sign up to request clarification or add additional context in comments.

5 Comments

thank you Xhynk for this fantastic explanation this is an answer beyond my expectations clean and easy to understand code! I spent 1h testing, in the beginning it didn't want to display the parent terms. So I found that because of "if ($ term-> parent === 0)" i changed it to "if ($ term-> parent! = 0)" for the moment I can't get [parent-child link = "false"] to work. there maybe another little error.
Oh shoot, sorry about the === that was from something I was doing earlier. I've updated that. I've also changed $link part to $link != false. You can also put var_dump( $link ) in there somewhere to make sure it's showing the value you want
hello, Xhynk, this is another task i tried to separate parent and childs, i failed this task is it possible to request a change to separate parent - child values here? I ask you the question before opening another topic maybe it's better another topic like that I put the link here? what do you think about it
You're probably better off asking another question since the answer here works for the question above, as it's written and could provide benefit to other users in its current state
thanks I opened another topic

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.