Home / Widgets / Post term count
Duplicate Snippet

Embed Snippet on Your Site

Post term count

<10
Code Preview
php
<?php
add_shortcode( 'term_count', function( $atts ) {
	$atts = shortcode_atts(
		array(
			'taxonomy'   => '',
			'hide_empty' => 'false',
		),
		$atts,
		'term_count'
	);
	$taxonomy = sanitize_key( $atts['taxonomy'] );
	if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
		return '0';
	}
	$terms = get_terms(
		array(
			'taxonomy'   => $taxonomy,
			'hide_empty' => filter_var(
				$atts['hide_empty'],
				FILTER_VALIDATE_BOOLEAN
			),
			'fields'     => 'ids',
		)
	);
	if ( is_wp_error( $terms ) ) {
		return '0';
	}
	return (string) count( $terms );
} );

Comments

Add a Comment