首先,我认为您必须为开始周设置一个配置:即使用菜单1开始的第一周。也许还可以配置开始日期,这样您就可以选择菜单更改的星期几。
我的建议是设置2个配置:$start_date
和$menu_duration
, 通过这种方式,您的代码更加灵活。
之后,您可以使用一些逻辑来:
获取并计算所有菜单(现在您有2个菜单,但将来可能会更多)
计算当前日期和下一个菜单(使用开始日期)还计算菜单更改的日期可以将其代码放在插件中,配置可以放在自定义管理页面中,但为了得到正确的答案,我将把它放在一个页面模板文件中。我假设您的自定义帖子类型命名为“菜单”。
<?php
/**
* Template Name: Menu Program
*
*/
?>
<?php
/* config */
$start_date = \'2013/01/01\'; // yyyy/mm/dd
$menu_duration = 7; // days
/* logic */
$menus = get_post(\'post_type=menus&posts_per_page=-1&orderby=date&order=ASC\');
$menunum = count($menus);
$start_a = explode(\'/\', $start);
$start_ts = mktime(1,0,0, (int)$start_a[1], (int)$start_a[2], (int)$start_a[0]);
$differ = (time() - $start_ts) / ( DAY_IN_SECONDS * $menu_duration );
$week_now = ceil ( $differ );
$untill = ( ($week_now+1) - $differ ) * ( DAY_IN_SECONDS * $menu_duration );
$untill_date = date_i18n( get_option(\'date_format\'), $untill);
$next_week_index = $week_now % $menunum;
$current_week_index = $next_week_index-1 >= 0 ? $next_week_index-1 : $menunum-1;
$current_week_menu = $menus[$current_week_index];
$next_week_menu = $menus[$next_week_index];
/* display */
echo \'<div>\';
printf ( \'<p>\' . __(\'This week, untill %s, we are serving menu: %s\') . \'</p>\', $untill_date, apply_filters(\'the_title\', $current_week_menu->post_title) );
echo \'<p>\' . apply_filters(\'the_content\', $current_week_menu->post_content) . \'</p>\';
echo \'</div>\';
echo \'<div>\';
printf ( \'<p>\' . __(\'Starting from %s we are serving menu: %s\') . \'</p>\', $untill_date, apply_filters(\'the_title\', $next_week_menu->post_title) );
echo \'<p>\' . apply_filters(\'the_content\', $next_week_menu->post_content) . \'</p>\';
echo \'</div>\';
?>
当然,显示部分只是一个axample。请注意,代码是全部untested.