Skip Navigation

[Résolu] Conditional Output If Not Blank Statement

This support ticket is created Il y a 4 années et 1 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Marqué : 

This topic contains 3 réponses, has 2 voix.

Last updated by Shane Il y a 4 années et 1 mois.

Assisted by: Shane.

Auteur
Publications
#1542309

Tell us what you are trying to do?
I'm sure there's an easy way to do this but I didn't have luck searching. I display the wordpress tags/categories on my custom post type.

```
<p class="category uk-heading-bullet uk-margin-remove">[wpv-post-taxonomy type="project-category" format="name"]</p>
<p>Tagged: [wpv-post-taxonomy type="project-tag"]</p>
```

I would simply want to wrap each in a statement that if the WordPress Tags/Categories are != blank, it will display, and if not it will be hidden from being output.

For other fields like the Featured Image I can do

```
[wpv-conditional if="( '[wpv-post-featured-image output="url"]' ne '' )"]
```

But the Conditional thing only lets me choose "includes the term" or "doesn't include the term" and some of the categories for example have spaces so I believe it would fire incorrectly.

Is there a standard "if blank" that could be used for categories and tags?

Is there any documentation that you are following?
None that I could find

Is there a similar example that we can see?
When using the View editor and clicking "Conditional Output" you can choose != for any other fields

What is the link to your site?
N/a

#1542473

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Chris,

Thank you for getting in touch.

Taking a look at your query its not so clear to me what you are trying to achieve.
"I would simply want to wrap each in a statement that if the WordPress Tags/Categories are != blank, it will display, and if not it will be hidden from being output."

Is it that you want to check if a post has taxonomies attached to it ? if so please let me know and we can go from there.

Thanks,
Shane

#1542609

Hi Shane,

That is correct. If there are tags on the post it should output the HTML paragraph and list of tags, but if there are no tags, nothing should be displayed.

Then I want to do the same for category as well.

Thanks!

#1545713

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Chris,

Thank you for the continued patience.

I've draft up a simple shortcode that should get this working for you.

// Add Shortcode
function wp_check_for_terms( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'post_id' => '',
			'taxonomy' => '',
		),
		$atts
	);

	$terms = get_the_terms($atts['post_id'],$atts['taxonomy']);
	
	if (empty($terms)){
	    return 0;
	}else{
	    return 1;
	}

}
add_shortcode( 'wp_check_for_terms', 'wp_check_for_terms' );

Add this to your toolset custom code in Toolset -> Settings -> Custom Code and ensure its activated.

So what this shortcode does is to check your post to see if it has any terms from a taxonomy assigned and if it does it returns 0 or 1 based on if there are terms or not.

An example of the shortcode format is [wp_check_for_terms post_id='[wpv-post-id]' taxonomy='post_tag']. This will check if there are any tags assigned to the post.

In your case you would have


[wpv-conditional if="( '[wp_check_for_terms post_id='[wpv-post-id]' taxonomy='post_tag']]' eq '0' ]

Display something

[/wpv-conditional]

NB: In order for this custom shortcode to work in our conditional you must add the shortcode name to our Toolset 3rd party shortcode arguments in Toolset -> Settings -> Frontend.

Please let me know if this helps.
Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.