在查看了许多收视率插件之后,我决定让事情更简单一些,只需使用ACF和自定义帖子类型来构建自己的收视率插件。到目前为止,一切都按预期进行,但我觉得我的代码可以更加干净和有条理。
我使用根;我已经创建了一个自定义的帖子类型模板,并且正在对我的评分字段(这是一个浮点;例如4.5)进行preg\\u替换,以删除小数,这样我就可以轻松地回显css类中的原始数字,如下所示:
$ratings_overall = get_field(\'provider_ratings_overall\');
$ratings_round_overall = preg_replace("/[^0-9]/", \'\', $ratings_overall);
echo \'<div class="class-name-\' . $ratings_round_overall . \'">
我的问题是,这对我来说并不干净,我的函数文件现在有一个长得离谱的函数字符串,其中包含if/elseif语句,以便填充分级(这些存储在每个分级的模板中,其中只包含我需要在页面上显示星星的html)。
<div class="rating-stars">
<span class="five-stars">
<span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
<span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
<span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
<span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
<span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
</span>
</div>
我的脏函数文件:
/**
* Functions for the Review post type
*/
function rating_overall() {
$ratings_overall = get_field(\'provider_ratings_overall\');
if ($ratings_overall == 5.0) {
echo get_template_part(\'templates/star-rating/five-star-rating\');
}
elseif ($ratings_overall == 4.5) {
echo get_template_part(\'templates/star-rating/four-half-star-rating\');
}
elseif ($ratings_overall == 4.0) {
echo get_template_part(\'templates/star-rating/four-star-rating\');
}
elseif ($ratings_overall == 3.5) {
echo get_template_part(\'templates/star-rating/three-half-star-rating\');
}
elseif ($ratings_overall == 3.0) {
echo get_template_part(\'templates/star-rating/three-star-rating\');
}
elseif ($ratings_overall == 2.5) {
echo get_template_part(\'templates/star-rating/two-half-star-rating\');
}
elseif ($ratings_overall == 2.0) {
echo get_template_part(\'templates/star-rating/two-star-rating\');
}
elseif ($ratings_overall == 1.5) {
echo get_template_part(\'templates/star-rating/one-half-star-rating\');
}
elseif ($ratings_overall == 1.0) {
echo get_template_part(\'templates/star-rating/one-star-rating\');
}
elseif ($ratings_overall == 0.5) {
echo get_template_part(\'templates/star-rating/half-star-rating\');
}
elseif ($ratings_overall == \'\') {
echo get_template_part(\'templates/star-rating/no-star-rating\');
}
}
function rating_marketing() {
$ratings_marketing = get_field(\'provider_ratings_marketing\');
if ($ratings_marketing == 5.0) {
echo get_template_part(\'templates/star-rating/five-star-rating\');
}
elseif ($ratings_marketing == 4.5) {
echo get_template_part(\'templates/star-rating/four-half-star-rating\');
}
elseif ($ratings_marketing == 4.0) {
echo get_template_part(\'templates/star-rating/four-star-rating\');
}
elseif ($ratings_marketing == 3.5) {
echo get_template_part(\'templates/star-rating/three-half-star-rating\');
}
elseif ($ratings_marketing == 3.0) {
echo get_template_part(\'templates/star-rating/three-star-rating\');
}
elseif ($ratings_marketing == 2.5) {
echo get_template_part(\'templates/star-rating/two-half-star-rating\');
}
elseif ($ratings_marketing == 2.0) {
echo get_template_part(\'templates/star-rating/two-star-rating\');
}
elseif ($ratings_marketing == 1.5) {
echo get_template_part(\'templates/star-rating/one-half-star-rating\');
}
elseif ($ratings_marketing == 1.0) {
echo get_template_part(\'templates/star-rating/one-star-rating\');
}
elseif ($ratings_marketing == 0.5) {
echo get_template_part(\'templates/star-rating/half-star-rating\');
}
elseif ($ratings_marketing == \'\') {
echo get_template_part(\'templates/star-rating/no-star-rating\');
}
}
function rating_sales() {
$ratings_sales = get_field(\'provider_ratings_sales\');
if ($ratings_sales == 5.0) {
echo get_template_part(\'templates/star-rating/five-star-rating\');
}
elseif ($ratings_sales == 4.5) {
echo get_template_part(\'templates/star-rating/four-half-star-rating\');
}
elseif ($ratings_sales == 4.0) {
echo get_template_part(\'templates/star-rating/four-star-rating\');
}
elseif ($ratings_sales == 3.5) {
echo get_template_part(\'templates/star-rating/three-half-star-rating\');
}
elseif ($ratings_sales == 3.0) {
echo get_template_part(\'templates/star-rating/three-star-rating\');
}
elseif ($ratings_sales == 2.5) {
echo get_template_part(\'templates/star-rating/two-half-star-rating\');
}
elseif ($ratings_sales == 2.0) {
echo get_template_part(\'templates/star-rating/two-star-rating\');
}
elseif ($ratings_sales == 1.5) {
echo get_template_part(\'templates/star-rating/one-half-star-rating\');
}
elseif ($ratings_sales == 1.0) {
echo get_template_part(\'templates/star-rating/one-star-rating\');
}
elseif ($ratings_sales == 0.5) {
echo get_template_part(\'templates/star-rating/half-star-rating\');
}
elseif ($ratings_sales == \'\') {
echo get_template_part(\'templates/star-rating/no-star-rating\');
}
}
function rating_support() {
$ratings_support = get_field(\'provider_ratings_support\');
if ($ratings_support == 5.0) {
echo get_template_part(\'templates/star-rating/five-star-rating\');
}
elseif ($ratings_support == 4.5) {
echo get_template_part(\'templates/star-rating/four-half-star-rating\');
}
elseif ($ratings_support == 4.0) {
echo get_template_part(\'templates/star-rating/four-star-rating\');
}
elseif ($ratings_support == 3.5) {
echo get_template_part(\'templates/star-rating/three-half-star-rating\');
}
elseif ($ratings_support == 3.0) {
echo get_template_part(\'templates/star-rating/three-star-rating\');
}
elseif ($ratings_support == 2.5) {
echo get_template_part(\'templates/star-rating/two-half-star-rating\');
}
elseif ($ratings_support == 2.0) {
echo get_template_part(\'templates/star-rating/two-star-rating\');
}
elseif ($ratings_support == 1.5) {
echo get_template_part(\'templates/star-rating/one-half-star-rating\');
}
elseif ($ratings_support == 1.0) {
echo get_template_part(\'templates/star-rating/one-star-rating\');
}
elseif ($ratings_support == 0.5) {
echo get_template_part(\'templates/star-rating/half-star-rating\');
}
elseif ($ratings_support == \'\') {
echo get_template_part(\'templates/star-rating/no-star-rating\');
}
}
function rating_pricing() {
$ratings_pricing = get_field(\'provider_ratings_pricing\');
if ($ratings_pricing == 5.0) {
echo get_template_part(\'templates/star-rating/five-star-rating\');
}
elseif ($ratings_pricing == 4.5) {
echo get_template_part(\'templates/star-rating/four-half-star-rating\');
}
elseif ($ratings_pricing == 4.0) {
echo get_template_part(\'templates/star-rating/four-star-rating\');
}
elseif ($ratings_pricing == 3.5) {
echo get_template_part(\'templates/star-rating/three-half-star-rating\');
}
elseif ($ratings_pricing == 3.0) {
echo get_template_part(\'templates/star-rating/three-star-rating\');
}
elseif ($ratings_pricing == 2.5) {
echo get_template_part(\'templates/star-rating/two-half-star-rating\');
}
elseif ($ratings_pricing == 2.0) {
echo get_template_part(\'templates/star-rating/two-star-rating\');
}
elseif ($ratings_pricing == 1.5) {
echo get_template_part(\'templates/star-rating/one-half-star-rating\');
}
elseif ($ratings_pricing == 1.0) {
echo get_template_part(\'templates/star-rating/one-star-rating\');
}
elseif ($ratings_pricing == 0.5) {
echo get_template_part(\'templates/star-rating/half-star-rating\');
}
elseif ($ratings_pricing == \'\') {
echo get_template_part(\'templates/star-rating/no-star-rating\');
}
}
然后,我会在我的模板中调用这些内容,无论我在哪里需要它们:
rating_overall();
rating_marketing();
rating_sales();
rating_support();
rating_pricing();
我正试图尽可能地解决这个问题,所以我想这更像是一个PHP重构问题,但我也觉得在Wordpress中,或者使用一些Wordpress编码标准,可能有更好的方法来实现这一点?
任何帮助都将不胜感激。