admin管理员组文章数量:1417070
i hope to add span tag or class at sprintf
I want to enlarge the text size of the numbers in front of me. So I want to add span tag or class only to text in front of me.
my code is
/**
* Filters the product average rating.
*
* @since 1.10.0
*
* @param float $average_rating average rating
* @param \WC_Product $product the product object
*/
$average_rating = max( 0, (float) apply_filters( 'wc_product_reviews_pro_product_rating_average', $product->get_average_rating(), $product ) );
$reviews_count = max( 0, wc_product_reviews_pro_get_contributions_count( $product, 'review' ) );
/* translators: Placeholders: %s - average rating stars count (float casted as string to avoid trailing zeroes), %d - 5 stars total (integer) -- (e.g "4.2 / 5 stars") */
$reviews_label = sprintf( __( '%s / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 );
?>
<h3><?php echo esc_html( $reviews_label ); ?></h3>
<p><?php echo get_star_rating(); ?></p>
I tried many ways. I've taken a lot of reference to the above url
But all the results are printed like <span>4</span>/5
.
I don't want it to be in print.
Is there a good solution? thanks.
i hope to add span tag or class at sprintf
I want to enlarge the text size of the numbers in front of me. So I want to add span tag or class only to text in front of me.
my code is
/**
* Filters the product average rating.
*
* @since 1.10.0
*
* @param float $average_rating average rating
* @param \WC_Product $product the product object
*/
$average_rating = max( 0, (float) apply_filters( 'wc_product_reviews_pro_product_rating_average', $product->get_average_rating(), $product ) );
$reviews_count = max( 0, wc_product_reviews_pro_get_contributions_count( $product, 'review' ) );
/* translators: Placeholders: %s - average rating stars count (float casted as string to avoid trailing zeroes), %d - 5 stars total (integer) -- (e.g "4.2 / 5 stars") */
$reviews_label = sprintf( __( '%s / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 );
?>
<h3><?php echo esc_html( $reviews_label ); ?></h3>
<p><?php echo get_star_rating(); ?></p>
I tried many ways. https://stackoverflow/questions/45312511/adding-class-to-element-in-php-code I've taken a lot of reference to the above url
But all the results are printed like <span>4</span>/5
.
I don't want it to be in print.
Is there a good solution? thanks.
Share Improve this question asked Aug 8, 2019 at 4:36 Kt HKt H 152 silver badges6 bronze badges 4 |1 Answer
Reset to default 1Actaully using esc_html
won't help because it will escape any html character, so you have to depend on wp_kses
so you can specify the accepted tags and there attributes, so no need then to escape the output. but you have to escape the values comming from DB, which in you case $average_rating
. so your can be like this.
$accepted_tags = array('strong'=>array());
$reviews_label = wp_kses(
sprintf(
__( '%s / %d', '<strong>woocommerce-product-reviews-pro</strong>' ),
esc_html($average_rating),
5
),
$accepted_tags );
<h3><?php echo $reviews_label ; ?></h3>
Update
As i can see from your comment, You need to add tag to the %s
, so you need to just change the $accepted_tags
to:
$accepted_tags = array('span'=>array());
anf the sprintf
to:
sprintf(
__( '<span>%s</span> / %d', 'woocommerce-product-reviews-pro' ),
esc_html($average_rating),
5
),
本文标签: phphow can i add class or span tag at sprintf
版权声明:本文标题:php - how can i add class or span tag at sprintf? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745260500a2650335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$reviews_label = sprintf( __( '<span>%s</span> / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 );
and$reviews_label = <span> . sprintf( __( '%s</span> / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 );
But it doesn't work the way I want it to. – Kt H Commented Aug 8, 2019 at 6:01