I Have this snippet code,
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
extract( 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
* $link 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 ($link != false) ? sprintf( '<a href="%s">%s</a>, ', esc_url( get_term_link($parent_term) ), $parent_term->name ) : "{$parent_term->name}, " ;
}
echo ($link != false) ? sprintf( '<a href="%s">%s</a>', esc_url( get_term_link($term) ), $term->name ) : $term->name ;
}
return ob_get_clean();
}
This will allow you to get the following results: usage
**[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
Everything works fine and displays as expected! just...my parameters do not work.
I failed to get [parent-child link="false"] work.. During my research on how shortcodes work, I came across these posts,
they all talk about never using extract and echo in shortcodes this is so bad practice in wordpress code and using $atts and $return instead.
which casts doubt on the effectiveness of this code...
My question now is : how to make this shortcode [parent-child] working with link=false , and no extract and echo in shortcode.
thank you if you understood what i'm talking about and knows how to make it more respectful of wordpress standards