下面是我如何仅使用一个自定义字段来完成这项工作的。我没有使用“Stars”或“Hearts”作为自定义字段名称,“1”、“2”、“3”等作为值,而是使用“Rating”作为名称,“1星”、“2星”、“3星”等(或用Hearts代替Stars)作为值。因为红心和星星的测量单位都是0到5,所以我可以只按评级的一个自定义字段值进行排序,然后解析出显示值。这是我的代码:
<?php
// setup the query
$args=\'&posts_per_page=\'.$con_review_num.\'&post_type=con_movie_reviews&paged=\'.$paged.\'&order=DESC&orderby=\'.$feedsort.\'&meta_key=Rating\';
$review_loop = new WP_Query($args);
if ($review_loop->have_posts()) : while ($review_loop->have_posts()) : $review_loop->the_post(); $postcount++;
// get the custom field value
$rating = get_post_meta($post->ID, "Rating", $single = true);
$unit = "star"; //default unit is star
if(strpos($rating," Hearts") || strpos($rating," hearts") || strpos($rating," Heart") || strpos($rating," heart")) {
$unit = "heart"; //find out if we\'re using hearts instead of stars
}
// extract just the number from the rating meta key
$rating = trim(str_replace(" star","",str_replace(" stars","",str_replace(" Star","",str_replace(" Stars","",str_replace(" heart","",str_replace(" hearts","",str_replace(" Heart","",str_replace(" Hearts","",$rating)))))))));
if($stars) { // are we using stars or hearts?
$rating = $stars;
$unit = "star";
} elseif($hearts) {
$rating = $hearts;
$unit = "heart";
}
?>
<div class="stars tooltip" title="<?php echo $rating; ?> / 5 <?php echo $overall; ?>">
<div class="<?php echo $unit; ?><?php if($rating>=1) { ?> full<?php } elseif($rating>0) { ?> half<?php } ?>"> </div>
<div class="<?php echo $unit; ?><?php if($rating>=2) { ?> full<?php } elseif($rating>1) { ?> half<?php } ?>"> </div>
<div class="<?php echo $unit; ?><?php if($rating>=3) { ?> full<?php } elseif($rating>2) { ?> half<?php } ?>"> </div>
<div class="<?php echo $unit; ?><?php if($rating>=4) { ?> full<?php } elseif($rating>3) { ?> half<?php } ?>"> </div>
<div class="<?php echo $unit; ?><?php if($rating>=5) { ?> full<?php } elseif($rating>4) { ?> half<?php } ?>"> </div>
</div>
<?php
}
endwhile;
endif;
?>