最近,我一直在为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\', \' \', \' \', \'\' );
}
}
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),则可以将其缩短。