Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

Wordpress Insert Parameters In Shortcode?

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

question from:https://stackoverflow.com/questions/65598801/wordpress-insert-parameters-in-shortcode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...