Display different custom post type each week

I have a project where I need to display an alternating food menu each week.

  • i.e. menu1 on week1, menu2 on week2 then back to week1 etc.
  • I’ve created custom post types for each menu (menu1 & menu2).

Ideally, if the user wanted to see what the food menu was going to be next week, they would click on a calendar day a week from today & the food menu would display week2’s food menu.

Read More

I could deal with changing the food menu per week by having different custom pages to pull the different custom post types, but this does not solve the above requirement of what was on next week.

Related posts

2 comments

  1. You can use PHP’s date functionality:

    // set (or retrieve) the number of menus
    $num_menus = 2;
    // calculate the current/next menu according to the current/next week (number)
    $current_menu = date('W') % $num_menus;
    $next_menu = (date('W') + 1) % $num_menus;
    

    All you have to do then, is to map the numbers to the according menus (i.e., custom post types).

  2. Firt of all I think you have to set a configuration for the start week: i.e. the first week you start with menu 1.
    Probably is better also configure a day of start, in this way you can choose the day of week in which the menu change.

    My proposal is to set 2 configuration: $start_date and $menu_duration, in this way you your code is more flexible.

    After that you can use some logic to:

    1. Get and count all menus (now you have 2 menus, but in the futer, mayme can be more)
    2. Calculate for present date which is current and next menu (making use of start date)
    3. Also calculate the date in which the menu will change

    The code for that can be placed in a plugin and the configuration can be placed in a custom admin page, but for semplicity of answer I will put it in a page template file.

    I assume here that your custom post type is named ‘menus’.

    <?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>';
    ?>
    

    Of course, the display part is just an axample.

    Note that code is all untested.

Comments are closed.