问题
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..
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.
*/
ob_start();
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();
}
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
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.
回答1:
You could consider using an array()
instead to store the $parent
if it exists, and then the $child
which should exist. This would give you the benefit of being able to use array_reverse() to flip the order of the terms based on a parameter saying whether you want the parent or the child first. This gives you the added benefit of being able to convert the array to a string with the implode() function, which you can pass a $glue
argument to, to determine whether you want them separated by a space, comma + space, hyphen, slash, etc.
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',
'glue' => ', ',
'taxonomy' => 'property_city',
'first' => 'parent',
), $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.
*/
$array = array();
foreach( $terms as $term ){
if( $term->parent != 0 ){
$parent_term = get_term( $term->parent, $taxonomy );
if ($atts['link'] !== 'false') {
$array[] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link($parent_term) ), $parent_term->name );
} else {
$array[] = $parent_term->name;
}
}
if( $atts['link'] !== 'false' ){
$array = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link($term) ), $term->name );
} else {
$array = $term->name;
}
}
if( $atts['first'] != 'parent' ){
$array = array_reverse( $array );
}
return implode($glue, $array);
}
Doing this should give you the ability to put out shortcodes like:
[parent-child first="child" glue=" - "] // Child - Parent
[parent-child glue="/"] // Parent/Child
Documentation & Function Reference
Function | Linked Description |
---|---|
array_reverse() |
Return an array with elements in reverse order |
implode() |
Join array elements with a string |
回答2:
I think I found a solution but in this case it is no longer a shortcode with parameters. it's become a bunch of shortcodes with a parameters, at least we have more flexibility now.
I simply cut the code into 3 parts if you are not afraid to see 😰 very long complicated shortcodes. it's a solution that will help.
but I'm not sure if this is the best practice.
output taxonomy parent, child
/*output taxonomy parent, child*/
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
$atts = shortcode_atts( array(
'link' => 'true',
'taxonomy' => 'property_city' // my default taxonomy
), $atts, 'parent-child' );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
ob_start();
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();
}
output only taxonomy parent
/*output only taxonomy parent*/
add_shortcode( 'parent', 'taxonomy_parent' );
function taxonomy_parent( $atts ){
$atts = shortcode_atts( array(
'link' => 'true',
'taxonomy' => 'property_city' // my default taxonomy
), $atts, 'parent' );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
ob_start();
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;
}
}
}
return ob_get_clean();
}
output only taxonomy child
/*output only taxonomy child*/
add_shortcode( 'child', 'taxonomy_child' );
function taxonomy_child( $atts ){
$atts = shortcode_atts( array(
'link' => 'true',
'taxonomy' => 'property_city' // my default taxonomy
), $atts, 'child' );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
ob_start();
foreach( $terms as $term ){
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();
}
来源:https://stackoverflow.com/questions/65727060/wordpress-improve-shortcode-with-more-parameters