Hi Waqar, good day.
I am trying to create a post using WordPress tag taxonomies under conditional codes. but i realize it works only on first event, not all.
example my conditional code as this
This is [wpv-conditional if="( has_term('tags', 'product-tag', null) eq '1' )" ]tag[/wpv-conditional] .
This is [wpv-conditional if="( has_term('tags 2', 'product-tag', null) eq '1' )" ]tag 2[/wpv-conditional]
This is [wpv-conditional if="( has_term('tags 3', 'product-tag', null) eq '1' )" ]tag 3[/wpv-conditional]
do you know whats the issue ? Do i need to create view for this ? Thanks
ok, i found the issue, the tag is being converted into capital letters . from luxury > Luxury . is this normal ?
if i were to use many conditional code for tags will it affect the performance of the site, loading etc ?
thank you .
Hi Dee,
Thank you for contacting us and I'd be happy to assist.
Have you checked if the actual title of the tag term is saved with first letter capital or not in WordPress?
To avoid a situation like this, you can also use the slug of the terms instead of the titles, which is always in all lowercase.
( ref: https://codex.wordpress.org/Function_Reference/has_term )
The number of conditional blocks that you'll have with "has_term" function, will result in more processing time. To optimize this, you can use the "get_the_tags" function ( ref: https://developer.wordpress.org/reference/functions/get_the_tags/ ) to get all attached tag terms in a custom shortcode at once and then generate the markup/HTML accordingly.
I hope this helps.
regards,
Waqar
Hi wafar, im glad i asked you abt the performance issue. thank you for the explanations.
so i need changes, 1. use slug, 2. use get_the_tags
for the updated code, this will work ?
This is [wpv-conditional if="( get_the_tags('tags', 'product-tag', null) eq '1' )" ]tag[/wpv-conditional] .
This is [wpv-conditional if="( get_the_tags('tags 2', 'product-tag', null) eq '1' )" ]tag 2[/wpv-conditional]
This is [wpv-conditional if="( get_the_tags('tags 3', 'product-tag', null) eq '1' )" ]tag 3[/wpv-conditional]
it seems like i would need a custom shortcode fn for this.
Thanks
Hi Dee,
Thanks for writing back.
1. If your requirement is to check whether an individual tag term is attached or not, then you'll continue on using your current approach of using the "has_term" function in a conditional block.
I suggested using tag term's slug instead of a name because slug is in all small letters and empty spaces are replaced by "-".
( example screenshot: hidden link )
For example, your code:
This is [wpv-conditional if="( has_term('tags 2', 'product-tag', null) eq '1' )" ]tag 2[/wpv-conditional]
Will become:
This is [wpv-conditional if="( has_term('tags-2', 'product-tag', null) eq '1' )" ]tag 2[/wpv-conditional]
Since the slug of your tag with name "tags 2" or "Tags 2" will most likely be "tags-2".
2. On the other hand, if your goal is to show all the attached tag terms to a post, you can show them all at once, without having to add a conditional check for each term.
Example shortcode:
function show_attached_tags_func( $atts ) {
$a = shortcode_atts( array(
'tax' => '',
), $atts );
if( !empty($a['tax']) ) {
$terms = get_the_terms( get_the_ID(), $a['tax'] );
if ( $terms && ! is_wp_error( $terms ) ) {
ob_start();
foreach ( $terms as $term ) {
echo '<p>This is '.$term->name.'</p>';
}
return ob_get_clean();
}
}
}
add_shortcode( 'show_attached_tags', 'show_attached_tags_func' );
To show the terms from product tags taxonomy in your view, you'll be able to use the shortcode inside your view's loop like this:
[show_attached_tags tax="product-tag"]
Note: I've used "get_the_terms" function in the example instead of the "get_the_tags", because the "get_the_tags" only works with the post tags taxonomy.
I hope this clarifies.
regards,
Waqar
Hi , i wont need the second fn as i am selecting few tags to create a full sentences.
example:
This product has 'tag1' , but 'tag2' is an option too. Don't worry we have 'tag3' if you prefer.
Thank You,
Next reply i will close this tickets, thanks
Hi Dee,
Thanks for clarifying that and yes, for a consolidated statement involving multiple tags, your current approach (of using multiple conditional blocks) is the way to go.
You're welcome to close this ticket and open a new one for a different issue/question.
regards,
Waqar
My issue is resolved now. Thank you!