为什么这个短代码在页面上显示两次?
Why is this shortcode displayed twice on the page?
我正在尝试在页面上添加评级简码,例如 [rating rating="3"],但问题是它显示了两次。
function rating_function($atts) {
extract(shortcode_atts(array(
'rating' => 5,
'type' => 'rating',
'number' => 0
), $atts));
return wp_star_rating(array('rating' => $rating, 'type' => $type, 'number' => $number));
}
add_shortcode('rating', 'rating_function');
您两次看到评级的原因是因为默认情况下 wp_star_rating() 函数回显生成的评级 HTML 标记。来自文档:
$args (array) (Optional) Array of star ratings arguments.
- 'rating' (int|float) The rating to display, expressed in either a 0.5 rating increment, or percentage. Default 0.
- 'type' (string) Format that the $rating is in. Valid values are 'rating' (default), or, 'percent'. Default 'rating'.
- 'number' (int) The number of ratings that makes up this rating. Default 0.
- 'echo' (bool) Whether to echo the generated markup. False to return the markup instead of echoing it. Default true.
Default value: array()
(强调我的。)
在短代码中使用此函数时,必须确保函数 returns 是 HTML 标记,而不是 echo它:
function rating_function($atts) {
require_once(ABSPATH . 'wp-admin/includes/template.php');
extract(shortcode_atts(array(
'rating' => 5,
'type' => 'rating',
'number' => 0
), $atts));
return wp_star_rating(array(
'rating' => $rating,
'type' => $type,
'number' => $number,
'echo' => false
));
}
add_shortcode('rating', 'rating_function');
我正在尝试在页面上添加评级简码,例如 [rating rating="3"],但问题是它显示了两次。
function rating_function($atts) {
extract(shortcode_atts(array(
'rating' => 5,
'type' => 'rating',
'number' => 0
), $atts));
return wp_star_rating(array('rating' => $rating, 'type' => $type, 'number' => $number));
}
add_shortcode('rating', 'rating_function');
您两次看到评级的原因是因为默认情况下 wp_star_rating() 函数回显生成的评级 HTML 标记。来自文档:
$args (array) (Optional) Array of star ratings arguments.
- 'rating' (int|float) The rating to display, expressed in either a 0.5 rating increment, or percentage. Default 0.
- 'type' (string) Format that the $rating is in. Valid values are 'rating' (default), or, 'percent'. Default 'rating'.
- 'number' (int) The number of ratings that makes up this rating. Default 0.
- 'echo' (bool) Whether to echo the generated markup. False to return the markup instead of echoing it. Default true.
Default value: array()
(强调我的。)
在短代码中使用此函数时,必须确保函数 returns 是 HTML 标记,而不是 echo它:
function rating_function($atts) {
require_once(ABSPATH . 'wp-admin/includes/template.php');
extract(shortcode_atts(array(
'rating' => 5,
'type' => 'rating',
'number' => 0
), $atts));
return wp_star_rating(array(
'rating' => $rating,
'type' => $type,
'number' => $number,
'echo' => false
));
}
add_shortcode('rating', 'rating_function');