Skip Navigation

[Gelöst] When on a CPT use a conditional to find out if the number of terms are 1 OR more

This support ticket is created vor 2 Jahre, 9 Monate. 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by lindsayH vor 2 Jahre, 9 Monate.

Assisted by: Waqar.

Author
Artikel
#2081841
tern count.png

I have a CPT - Staff Profiles and each staff member can belong to more than one team (a term in a taxonomy).

Member of: [wpv-post-taxonomy type="team"] displays all the teams as I want. From that though I simply want to add the word teams or team depending on the number.

I've tried various solutions but nothing works, this is where I'm at so far (but not working).

<p class="no-boost"><strong>Member of:</strong> [wpv-post-taxonomy type="team"] [wpv-conditional if="('[wpv-post-taxonomy type='team' format='slug' ]' gt 1 )"]<strong>teams</strong>[/wpv-conditional][wpv-conditional if="('[wpv-post-taxonomy type='team' format='slug' ]' eq 1 )"]<strong>team</strong>[/wpv-conditional]</p>

Could you point me in the right direction. Thank you.

#2082389

Waqar
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To append some singular or plural text, based on the number of attached taxonomy terms, you can register a custom shortcode:


function singular_plural_txt_func( $atts, $content = null ) {
	$a = shortcode_atts( array(
		'sep' => ', ',
		'singular' => '',
		'plural' => '',
	), $atts );

	$content = apply_filters('the_content',$content);

	if(!empty($content)) {

		$content_arr = explode($a['sep'], $content);

		if(count($content_arr) >= 2 ) 
		{
			$final_content = $content.' '.$a['plural'];
		}
		else
		{
			$final_content = $content.' '.$a['singular'];
		}
		return $final_content;
	}
}

add_shortcode( 'singular_plural_txt', 'singular_plural_txt_func' );

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

After that you'll be able to use this new shortcode in your content like this:


[singular_plural_txt sep=", " singular="<strong>Team</strong>" plural="<strong>Teams</strong>"][wpv-post-taxonomy type="team"][/singular_plural_txt]

Please note how the original taxonomy shortcode ( [wpv-post-taxonomy type="team"] ) is enclosed in the new shortcode and the "sep", "singular" and "plural" values are passed in the shortcode attributes. You can adjust them for different taxonomies and singular and plural text.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2084023

My issue is resolved now. Thank you!

Excellent support, worked perfectly.

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