Skip Navigation

[Gelöst] Taxonomy count for a post

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

The issue here is that the user wanted to count the number of terms that is attached to a post.

Solution:

// Add Shortcode
function count_post_terms( $atts ) {
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
            'taxonomy' => '',
        ),
        $atts
    );
    $terms = wp_get_post_terms( $atts['post_id'], $atts['taxonomy']);
    return count($terms);
}

add_shortcode( 'count_post_terms', 'count_post_terms' );

Add the shortcode above to your functions.php file and you can use this shortcode [count_post_terms] to count the terms attached to a post.

The exact example is.

1
[count_post_terms post_id='[wpv-post-id]' taxonomy='category']
In order to use this in our views conditional shortcode you need to add it to our views 3rd party shortcode arguments in Toolset->Settings->Frontend Content.

This support ticket is created vor 6 Jahre, 2 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Tim Elliott vor 6 Jahre, 2 Monate.

Assistiert von: Shane.

Author
Artikel
#953561

Tell us what you are trying to do?
I have a post type "Products" which has a taxonomy "Brochures". Some products have a single brochure, some have more than one, some have none.

I need to format the content template depending on how many brochures there are.
i.e.
[wpv-conditional if="( [number_of_brochures] eq 0 )"]Do Something[/wpv-conditional]
[wpv-conditional if="( [number_of_brochures] eq 1 )"]Do Something else[/wpv-conditional]
[wpv-conditional if="( [number_of_brochures] eq 2 )"]Do Something different[/wpv-conditional]

Is there a shortcode I can use to get a count of the brochures for a particular product?

Is there any documentation that you are following?
I've looked through the shortcode documentation but there doesn't seem to be one for this.
[wpv-taxonomy-post-count] does it in reverse - counts post for a taxonomy but doesn't count taxonomies for a post.
[wpv-post-taxonomy type="product-brochure" show="count"] also counts how many posts with that taxonomy, not the number of taxonomies attached to current post.

Is there a similar example that we can see?

What is the link to your site?
versteckter Link but it's in development at the moment. I can provide access if needed.

Hopefully an easy one...

Thanks
Tim

#953710

Shane
Supporter

Sprachen: Englisch (English )

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

Hi Tim,

Thank you for contacting our support forum.

// Add Shortcode
function count_post_terms( $atts ) {
	// Attributes
	$atts = shortcode_atts(
		array(
			'post_id' => '',
			'taxonomy' => '',
		),
		$atts
	);
	$terms = wp_get_post_terms( $atts['post_id'], $atts['taxonomy']);
	return count($terms);
}
add_shortcode( 'count_post_terms', 'count_post_terms' );

Add the shortcode above to your functions.php file and you can use this shortcode [count_post_terms] to count the terms attached to a post.

The exact example is.

[count_post_terms post_id='[wpv-post-id]' taxonomy='category']

In order to use this in our views conditional shortcode you need to add it to our views 3rd party shrotcode arguments in Toolset->Settings->Frontend Content.

Thanks,
Shane

#954418

Thanks Shane. Perfect.

Dieses Ticket ist jetzt geschlossen. Wenn Sie ein Toolset Kunde sind und Hilfe benötigen, eröffnen Sie bitte ein neues Support-Ticket.