我知道你已经要求用js给出答案,但我无法对你是否接受用PHP编写的解决方案发表评论。
如果您喜欢使用PHP,可以创建一个接受每个短代码作为参数的短代码。您甚至可以使用他们的快捷码映射工具将其映射到visual composer中。
示例短代码如下所示:
[change_shortcode_each_day mon="short_one" tue="short_two" wed="short_three" thu="short_four" fri="short_five" sat="short_six" sun="short_seven" default="default_short"]
注意:我提供了一个默认选项,以防您每天都没有短代码。
add_shortcode(\'change_shortcode_each_day\', \'wpse_273181_change_shortcode_each_day\');
function wpse_273181_change_shortcode_each_day($atts) {
/**
* You can set defaults here so you don\'t have to right the
* args in the shortcode itself.
*
* You cannot technically use a shortcode in a shortcode
* so you need to type the shortcodes without the brackets
*/
$unprepared_shortcodes = shortcode_atts(
array(
\'mon\' => \'\',
\'tue\' => \'\',
\'wed\' => \'\',
\'thu\' => \'\',
\'fri\' => \'\',
\'sat\' => \'\',
\'sun\' => \'\',
\'default\' => \'\',
),
$atts
);
//make each shortcode \'complete\' e.g [short_one]
foreach ($unprepared_shortcodes as $key => $value) {
$prepared_shortcodes[$key] = \'[\' . $value . \']\';
}
extract($prepared_shortcodes);
$current_day = date(\'N\');
switch ($current_day) {
case \'1\' :
$short_to_use = $mon;
break;
case \'2\' :
$short_to_use = $tue;
break;
case \'3\' :
$short_to_use = $wed;
break;
case \'4\' :
$short_to_use = $thu;
break;
case \'5\' :
$short_to_use = $fri;
break;
case \'6\' :
$short_to_use = $sat;
break;
case \'7\' :
$short_to_use = $sun;
break;
}
/**
* check to make sure the we have a shortcode for the day
* if we don\'t use the default.
*
* return false if both short_to_use and default are \'blank\'
*/
if($short_to_use == \'[]\' && $default != \'[]\') {
$short_to_use = $default;
} else if ($short_to_use == \'[]\' && $default == \'[]\') {
return false;
}
//if we\'re still here do the shortcode
return do_shortcode($short_to_use);
}