如何在条件逻辑中使用“Case”而不是“IF”

时间:2012-10-29 作者:Ashkas

最近,我一直在为WordPress开发网站,这些网站越来越复杂,但流量也要高得多。我记得当我从另一个关于PHP的人那里学到诀窍时,在优化的情况下,“cases”是比“IFs”更好的选择,因为它们给服务器带来的负载更少。

我已经成功地在一些函数中使用了“cases”,但当它用于像single这样的文件时。php,那么我就惨败了。

所以我想我应该把它推广给社区,看看WordPress循环中的逻辑是否可以用“cases”优化。

下面的代码主要检查加载的内容是否是六种自定义帖子类型中的一种,并根据它是哪种帖子类型,将不同的参数输入到自定义函数中。

if (is_singular(\'news\')) {
    terms_by_order(\'news_category\', array(3623));
    if( false != get_the_term_list( $post->ID, \'news_column\' ) ) {
        echo \' | \' . get_the_term_list($post->ID,\'news_column\', \' \', \' \', \'\' );
    }
} 

/* <!-- if sport --> */
if (is_singular(\'sports\')) {
    terms_by_order(\'sports_category\', array(3623));
    if( false != get_the_term_list( $post->ID, \'sports_column\' ) ) {
       echo \' | \' . get_the_term_list($post->ID,\'sports_column\', \' \', \' \', \'\' );
    }
} 

/* <!-- if opinion --> */
if (is_singular(\'opinion\')) {
    terms_by_order(\'opinion_category\', array(3623));
    if( false != get_the_term_list( $post->ID, \'opinion_column\' ) ) {
        echo \' | \' . get_the_term_list($post->ID,\'opinion_column\', \' \', \' \', \'\' );
    }
} 

/* <!-- if life --> */
if (is_singular(\'life\')) {
    terms_by_order(\'life_category\', array(3623));
    if( false != get_the_term_list( $post->ID, \'life_column\' ) ) {
        echo \' | \' . get_the_term_list($post->ID,\'life_column\', \' \', \' \', \'\' );
    }
} 

/* <!-- if culture --> */
if (is_singular(\'culture\')) {
    terms_by_order(\'culture_category\', array(3623));
    if( false != get_the_term_list( $post->ID, \'culture_column\' ) ) {
        echo \' | \' . get_the_term_list($post->ID,\'culture_column\', \' \', \' \', \'\' );
    }
}


/* <!-- if community --> */
if (is_singular(\'community\')) {
    terms_by_order(\'community_category\', array(3623));
    if( false != get_the_term_list( $post->ID, \'community_column\' ) ) {
        echo \' | \' . get_the_term_list($post->ID,\'community_column\', \' \', \' \', \'\' );
    }
} 

2 个回复
最合适的回答,由SO网友:Mridul Aggarwal 整理而成

如果您在单个模板上使用它,最简单的;最有效的方法是根据get_post_type().

switch(get_post_type()) {
    case \'news\' : 
        // some statement for news type
    break;
    case \'sports\' :
        // some statement for sports type
    break;
    default:
        // something to do when it\'s none of the above
    break;
}
在我看来aforeach 这可能有点过头了(特别是如果有很多类似问题中的帖子类型的话)

SO网友:vstm

与其为每种情况编写代码,不如在数组中提取特定于帖子类型的信息,如类别名称或列名(也称为“数据”),并将其与逻辑(也称为“代码”)分离:

// here\'s the data - the key is the post type, the value is an array 
// with the column and category name (or any other post-type specific
// information you have).
$my_types = array(
    \'news\' => array(
        \'category\' => \'news_category\',
        \'column\' => \'news_column\',
        // ... other post type specific parameters
    ),
    \'sports\' => array(
        \'category\' => \'sports_category\',
        \'column\' => \'sports_column\',
    ),
    \'opinion\' => array(
        \'category\' => \'opinion_category\',
        \'column\' => \'opinion_column\',
    ),
    \'life\' => array(
        \'category\' => \'life_category\',
        \'column\' => \'life_column\',
    ),
    \'culture\' => array(
        \'category\' => \'culture_category\',
        \'column\' => \'culture_column\',
    ),
    \'community\' => array(
        \'category\' => \'community_category\',
        \'column\' => \'community_column\',
    ),
    // add more post types here
);

$resultlist = array();

// and here\'s the actual logic which is executed
// for every post type in the array.
foreach($my_types as $typename => $type) {
    if(is_singular($typename)) {
        terms_by_order($type[\'category\'], array(3623));

        // just call "get_the_term_list" once
        $list = get_the_term_list($post->ID, $type[\'column\'], \' \', \' \', \'\');

        // check if it is a valid list - get_the_term_list could
        // return false or a WP_Error.
        if(!($list === false || is_wp_error($list))) {
            // instead of directly "echo" the list, collect it in an array
            $resultlist[] = $list;
        }
    }
}

// echo the list with the separator - that way you don\'t have
// to manually track whether you have to echo a separator or not
echo implode(\' | \', $resultlist);
如果您确保类别名称或列名总是以相同的方式从post类型派生而来(如bla=>bla\\u category=>bla\\u column),则可以将其缩短。

结束

相关推荐