将这个 switch 语句变成一个函数(并将逻辑与内容分开)?

发布于 2024-11-30 13:25:26 字数 1579 浏览 0 评论 0原文

基本上,我想将以下 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2024-12-07 13:25:26

获取评级类别:

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';
}

打印评级类别:

function rating_class($thumbs_number) {
    echo get_rating_class($thumbs_number);
}

打印投票短语:

function votes($thumbs_number) {
    echo ($thumbs_number == 1 || $thumbs_number == -1) ? _e('vote') : _e('votes');
}

模板代码:

<?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>

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>
花开雨落又逢春i 2024-12-07 13:25:26
<?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 // 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); ?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文