Basically, I would like to turn the following switch-statement (which i开发者_C百科s in a Wordpress template file):
<?php preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
$thumbs_number = strip_tags( $n[1] ); ?>
<div class="topic-like-count
<?php // Apply style based on number of votes
switch ($thumbs_number) {
case ($thumbs_number == 0):
case ($thumbs_number == 1): echo ' average'; break;
case ($thumbs_number == 2):
case ($thumbs_number == 3): echo ' good'; break;
case ($thumbs_number == 4):
case ($thumbs_number == 5): echo ' great'; break;
case ($thumbs_number == 6):
case ($thumbs_number == 7): echo ' excellent'; break;
default:
if ($thumbs_number <= -1) echo "bad";
else if ($thumbs_number > 7) echo "brillant";
}
?>
">
<h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
<?php if ( $thumbs_number == 1 || $thumbs_number == -1 ) : ?>
<span><?php _e( 'vote' ); ?></span>
<?php else : ?>
<span><?php _e( 'votes' ); ?></span>
<?php endif; ?>
</div>
into a function (stored in functions.php) that I can use like this in a template:
<?php rating_class(); ?>
Any suggestions?
(The question has a bit of Worpdress but I think it is more a php question)
Get the rating class:
function get_rating_class($thumbs_number) {
if ($thumbs_number < 0) return 'bad';
if ($thumbs_number < 2) return 'average';
if ($thumbs_number < 4) return 'good';
if ($thumbs_number < 6) return 'great';
if ($thumbs_number < 8) return 'excellent';
return 'brillant';
}
Print the rating class:
function rating_class($thumbs_number) {
echo get_rating_class($thumbs_number);
}
Print the vote phrase:
function votes($thumbs_number) {
echo ($thumbs_number == 1 || $thumbs_number == -1) ? _e('vote') : _e('votes');
}
Template code:
<?php
preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
$thumbs_number = strip_tags( $n[1] );
?>
<div class="topic-like-count <?php rating_class($thumbs_number); ?>">
<h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
<span><?php votes($thumbs_number); ?></span>
</div>
<?php // Apply style based on number of votes
function rating_class($thumbs_number)
{
switch ($thumbs_number) {
case ($thumbs_number == 0):
case ($thumbs_number == 1): echo ' average'; break;
case ($thumbs_number == 2):
case ($thumbs_number == 3): echo ' good'; break;
case ($thumbs_number == 4):
case ($thumbs_number == 5): echo ' great'; break;
case ($thumbs_number == 6):
case ($thumbs_number == 7): echo ' excellent'; break;
default:
if ($thumbs_number <= -1) echo "bad";
else if ($thumbs_number > 7) echo "brillant";
}
}
?>
<?php rating_class($thumbs_number); ?>
精彩评论