根据星期几插入文本的简码

shortcode to insert text depending on day of week

我想创建一个 [time] 的简码,除了周六或周日外,每天都会显示 6:00-7:00AM,因为这两天会显示时间 8:00-9上午 00 点。我该怎么做?

这是我想出的...

// [time]  
function displaytime(){  
if (date('l') == 'Saturday' || date('l') == 'Sunday')){
   echo '8:00-9:00AM';
}else{ //it's a weekday
echo '6:00-7:00AM';
   }
}  
add_shortcode('time', 'displaytime'); 
// end display time

它当前的样子...(在周六和周日旁边应该显示 8:00-9;00AM) 我有 7 个简码来显示一周中接下来的 7 天。所以也许这就是导致它无法正常工作的原因?

// [tomorrow]  
function displaydate_tomorrow(){  
    return date('D, M j.', strtotime('+1 day')); 
}  
add_shortcode('tomorrow', 'displaydate_tomorrow'); 
// end tomorrows date
// [tomorrow2]  
function displaydate_tomorrow2(){  
    return date('D, M j.', strtotime('+2 day')); 
}  
add_shortcode('tomorrow2', 'displaydate_tomorrow2'); 
// end tomorrows date2
// [tomorrow3]  
function displaydate_tomorrow3(){  
    return date('D, M j.', strtotime('+3 day')); 
}  
add_shortcode('tomorrow3', 'displaydate_tomorrow3'); 
// end tomorrows date3
// [tomorrow4]  
function displaydate_tomorrow4(){  
    return date('D, M j.', strtotime('+4 day')); 
}  
add_shortcode('tomorrow4', 'displaydate_tomorrow4'); 
// end tomorrows date4
// [tomorrow5]  
function displaydate_tomorrow5(){  
    return date('D, M j.', strtotime('+5 day')); 
}  
add_shortcode('tomorrow5', 'displaydate_tomorrow5'); 
// end tomorrows date5
// [tomorrow6]  
function displaydate_tomorrow6(){  
    return date('D, M j.', strtotime('+6 day')); 
}  
add_shortcode('tomorrow6', 'displaydate_tomorrow6'); 
// end tomorrows date6
// [tomorrow7]  
function displaydate_tomorrow7(){  
    return date('D, M j.', strtotime('+7 day')); 
}  
add_shortcode('tomorrow7', 'displaydate_tomorrow7'); 
// end tomorrows date7
// [time]  
function displaytime(){  
    if (date('D') == 'Saturday' || date('D') == 'Sunday'){
        return '8:00-9:00AM';
    }else{ //it's a weekday
        return '6:00-7:00AM';
    }
}  
add_shortcode('time', 'displaytime'); 
// end display time

the docs 中说明:

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode.

尝试一下,如果有任何问题,请随时发表评论:

// [time]  
function displaytime(){  
    if (date('l') == 'Saturday' || date('l') == 'Sunday'){
        return '8:00-9:00AM';
    }else{ //it's a weekday
        return '6:00-7:00AM';
    }
}  
add_shortcode('time', 'displaytime'); 
// end display time

问题更新后编辑: 它在所有 7 个地方显示相同时间的原因是因为您的初始代码测试 date('l') 只考虑当天,所以它如果当天不是星期六或星期日,将到处显示“6:00-7:00AM”,否则到处显示“8:00-9:00AM”。除此之外,您正在创建 8 个不同的短代码,而 7 天只有 1 个就足够了,最后一个时间也应该合并,否则它无法知道我们考虑哪一天。

编辑 2:添加代码以设置函数的 wordpress 时区设置,然后恢复到函数之前的原始设置。

更新代码,所有代码都使用一个简码(现在您将学习如何向简码添加属性!):

// use examples: [nextdaytime day="+1 day"]  [nextdaytime day="+2 day"]
function displaynextdaytime($atts){
    $originalZone = date_default_timezone_get(); //save timezone
    date_default_timezone_set(get_option('timezone_string')); //set it to your admin value
    $time = strtotime($atts['day']); //time for the date() function based on our 'day' attribute
    if (date('l', $time) == 'Saturday' || date('l', $time) == 'Sunday'){
        $timeStr = '8:00-9:00AM';
    }else{ //it's a weekday
        $timeStr = '6:00-7:00AM';
    }
    $returnVal = date('D, M j.', $time) . ' ' . $timeStr;
    date_default_timezone_set($originalZone) //restore original zone
    return $returnVal;
}
add_shortcode('nextdaytime', 'displaynextdaytime'); 
// end display next day and time