返回介绍

wp_star_rating()

发布于 2017-09-11 12:57:46 字数 6159 浏览 893 评论 0 收藏 0

wp_star_rating( array $args = array() )

Output a HTML element with a star rating for a given rating.


description

Outputs a HTML element with the star rating exposed on a 0..5 scale in half star increments (ie. 1, 1.5, 2 stars). Optionally, if specified, the number of ratings may also be displayed by passing the $number parameter.


参数

$args

(array) (Optional) Array of star ratings arguments.

  • 'rating'
    (int) 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()


源代码

File: wp-admin/includes/template.php

function wp_star_rating( $args = array() ) {
	$defaults = array(
		'rating' => 0,
		'type'   => 'rating',
		'number' => 0,
		'echo'   => true,
	);
	$r = wp_parse_args( $args, $defaults );

	// Non-english decimal places when the $rating is coming from a string
	$rating = str_replace( ',', '.', $r['rating'] );

	// Convert Percentage to star rating, 0..5 in .5 increments
	if ( 'percent' == $r['type'] ) {
		$rating = round( $rating / 10, 0 ) / 2;
	}

	// Calculate the number of each type of star needed
	$full_stars = floor( $rating );
	$half_stars = ceil( $rating - $full_stars );
	$empty_stars = 5 - $full_stars - $half_stars;

	if ( $r['number'] ) {
		/* translators: 1: The rating, 2: The number of ratings */
		$format = _n( '%1$s rating based on %2$s rating', '%1$s rating based on %2$s ratings', $r['number'] );
		$title = sprintf( $format, number_format_i18n( $rating, 1 ), number_format_i18n( $r['number'] ) );
	} else {
		/* translators: 1: The rating */
		$title = sprintf( __( '%s rating' ), number_format_i18n( $rating, 1 ) );
	}

	$output = '<div class="star-rating">';
	$output .= '<span class="screen-reader-text">' . $title . '</span>';
	$output .= str_repeat( '<div class="star star-full" aria-hidden="true"></div>', $full_stars );
	$output .= str_repeat( '<div class="star star-half" aria-hidden="true"></div>', $half_stars );
	$output .= str_repeat( '<div class="star star-empty" aria-hidden="true"></div>', $empty_stars );
	$output .= '</div>';

	if ( $r['echo'] ) {
		echo $output;
	}

	return $output;
}

更新日志

Versiondescription
4.4.0Introduced the echo parameter.
3.8.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: _n()
  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/functions.php: number_format_i18n()

Used By

  • wp-admin/includes/class-wp-theme-install-list-table.php: WP_Theme_Install_List_Table::install_theme_info()
  • wp-admin/includes/plugin-install.php: install_plugin_information()
  • wp-admin/includes/class-wp-plugin-install-list-table.php: WP_Plugin_Install_List_Table::display_rows()
  • wp-admin/includes/ajax-actions.php: wp_ajax_query_themes()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Example

    
    <?php $args = array(
       'rating' => 3.5,
       'type' => 'rating',
       'number' => 1234,
    );
    wp_star_rating( $args ); ?>
    

    The above code outputs the following HTML:

    
    <div class="star-rating" title="3.5 rating based on 1,234 ratings">
    	<div class="star star-full"></div>
    	<div class="star star-full"></div>
    	<div class="star star-full"></div>
    	<div class="star star-half"></div>
    	<div class="star star-empty"></div>
    </div>
    
  2. Usage on the front end

    In order to use this function on the front end, your template must include the wp-admin/includes/template.php file and enqueue the appropriate dashicons CSS font information. Example CSS:

    
    @font-face {
    	font-family: "dashicons";
    	src: url("../fonts/dashicons.eot");
    }
    
    @font-face {
    	font-family: "dashicons";
    	src: url(data:application/x-font-woff;charset=utf-8;base64,/* !! Large amount of data removed, see wp-includes/css/dashicons.css for complete data !! */) format("woff"),
    		url("../fonts/dashicons.ttf") format("truetype"),
    		url("../fonts/dashicons.svg#dashicons") format("svg");
    	font-weight: normal;
    	font-style: normal;
    }
    
    .star-rating .star-full:before {
        content: "\f155";
    }
    
    .star-rating .star-half:before {
        content: "\f459";
    }
    
    .star-rating .star-empty:before {
        content: "\f154";
    }
    
    .star-rating .star {
        color:

    Note the font data in the above CSS has been omitted for clarity. This data must be included in working code. Refer to wp-admin/css/dashicons.css

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文