Skip Navigation

[Résolu] When on a CPT use a conditional to find out if the number of terms are 1 OR more

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.

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

Last updated by lindsayH Il y a 4 mois et 2 semaines.

Assigned support staff: Waqar.

Auteur
Publications
#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: Anglais (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.