将这个 switch 语句变成一个函数(并将逻辑与内容分开)?
基本上,我想将以下 switch 语句(位于 WordPress 模板文件中):
<?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>
转换为一个函数(存储在 functions.php 中),我可以在模板中像这样使用:
<代码>
有什么建议吗?
(这个问题有点Worpdress,但我认为这更多是一个php问题)
Basically, I would like to turn the following switch-statement (which is 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取评级类别:
打印评级类别:
打印投票短语:
模板代码:
Get the rating class:
Print the rating class:
Print the vote phrase:
Template code:
<?php rating_class($thumbs_number); ?>