简体   繁体   English

如何巧妙地组合多个条件语句

[英]How to neatly combine multiple conditional statements

I'm trying to add a sidebar widget to two different places. 我正在尝试将侧边栏小部件添加到两个不同的位置。 My code is working but there is duplication, so my question is how do I combine those statements? 我的代码可以运行,但是有重复,所以我的问题是如何合并这些语句?

            if( tribe_is_month() && !is_tax() ){ // Month View Page
                dynamic_sidebar( 'lavida_calendar' ); 
            } 
            elseif ( tribe_is_past() || tribe_is_upcoming() && ! is_tax() ) { // List View Page
                dynamic_sidebar( 'lavida_calendar' ); 
            }
            else {
            }

Since !is_tax exists in both branches, you can lead with that. 因为!is_tax在两个分支中都存在,所以您可以这样做。 Then, note all that remain are OR conditions: 然后,注意剩下的全部是OR条件:

if (!is_tax() && (tribe_is_month() || tribe_is_past() || tribe_is_upcoming())) {
    dynamic_sidebar(...);
}

This rewrite has the side effect of calling is_tax only once, though. 但是,此重写具有只调用一次is_tax

Hi Just set both conditions in one statement with OR condition 嗨,只需在一个带有OR条件的语句中设置两个条件

if( (tribe_is_month() && !is_tax())  || 
    (tribe_is_past() || tribe_is_upcoming() && ! is_tax() )){
    dynamic_sidebar( 'lavida_calendar' ); 
}

Also, it seens that this rule can be simplyfied, since it seens you want to show the sidebar for any month, past and upcoming which is not a tax 此外,它可以简化此规则,因为它可以显示任何月份,过去和将来的边栏,这不是税项

if ((tribe_is_month() || (tribe_is_past() || tribe_is_upcoming())
    && !is_tax()) {
    dynamic_sidebar( 'lavida_calendar' ); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM