Skip to content

Instantly share code, notes, and snippets.

@bohnna
Last active October 9, 2015 02:51
Show Gist options
  • Select an option

  • Save bohnna/ed6b585223a417c3ee20 to your computer and use it in GitHub Desktop.

Select an option

Save bohnna/ed6b585223a417c3ee20 to your computer and use it in GitHub Desktop.
Wordpress functions to gather store hours and group them according to hours, then output the results as a list, with an optional parameter for class name.
function get_hours_array() {
$shop_hours_array = array();
if (have_rows('hours') ) {
while (have_rows('hours') ) {
the_row();
$shop_hours_array[get_sub_field('acf_fc_layout')] = array(
'open' => get_sub_field('open_time'),
'close' => get_sub_field('close_time')
);
}
}
return $shop_hours_array;
}
function get_formatted_shop_hours() {
$shop_hours = get_hours_array();
$joined_hours = array();
$i = 0;
foreach ($shop_hours as $key => $item) {
$day_hours = array($item['open'], $item['close']);
if (empty($joined_hours)) {
$joined_hours[$i] = array($key => $day_hours);
} else {
$last_hours = $joined_hours[$i][$keyname];
if ($last_hours === $day_hours) {
$joined_hours[$i][$key] = $day_hours;
} else {
$i++;
$joined_hours[$i][$key] = $day_hours;
}
}
$keyname = $key;
}
return $joined_hours;
}
function output_shop_hours($class = null) {
$hours = get_formatted_shop_hours();
echo '<ul class="hour-list ' . $class . '">';
foreach($hours as $hour) {
$day = '<span class="day">' . substr(key($hour), 0, 3) . '</span>';
if (count($hour) > 1) {
$last_day = key( array_slice( $hour, -1, 1, TRUE) );
$day .= ' - ' . '<span>' . substr($last_day, 0, 3) . '</span>';
}
echo '<li class="days">' . $day . ':</li><li class="hours">' . $hour[key($hour)][0] . ' - ' . $hour[key($hour)][1] . '</li>';
}
echo '</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment