If Statements inside of an array
-
I’m trying to take the schema hours and make them more conditional. Here’s what I’m starting with. It works but I’d like to make each section conditional on if there is a value in the ‘open’ field. So I want none of the entire Sunday first section to show up if there is no value in ‘open-sun’:
$schema['openingHoursSpecification'] = [ [ '@type' => 'openingHoursSpecification', 'dayofWeek' => "Sunday", 'opens' => get_post_meta( $postId, 'open-sun', true ), 'closes' => get_post_meta( $postId, 'close-sun', true ) ], [ '@type' => 'openingHoursSpecification', 'dayofWeek' => "Monday", 'opens' => get_post_meta( $postId, 'open-mon', true ), 'closes' => get_post_meta( $postId, 'close-mon', true ) ], [ '@type' => 'openingHoursSpecification', 'dayofWeek' => "Tuesday", 'opens' => get_post_meta( $postId, 'open-tue', true ), 'closes' => get_post_meta( $postId, 'close-tue', true ) ], [ '@type' => 'openingHoursSpecification', 'dayofWeek' => "Wednesday", 'opens' => get_post_meta( $postId, 'open-wed', true ), 'closes' => get_post_meta( $postId, 'close-wed', true ) ], [ '@type' => 'openingHoursSpecification', 'dayofWeek' => "Thursday", 'opens' => get_post_meta( $postId, 'open-thu', true ), 'closes' => get_post_meta( $postId, 'close-thu', true ) ], [ '@type' => 'openingHoursSpecification', 'dayofWeek' => "Friday", 'opens' => get_post_meta( $postId, 'open-fri', true ), 'closes' => get_post_meta( $postId, 'close-fri', true ) ], [ '@type' => 'openingHoursSpecification', 'dayofWeek' => "Saturday", 'opens' => get_post_meta( $postId, 'open-sat', true ), 'closes' => get_post_meta( $postId, 'close-sat', true ) ], ];
So I’ve been trying to get each section of the array to only show if the ‘opens’ has a value. So it would only show the section for Sunday if the open-sun field has a value.
Here’s what I was trying:
$open_sun = get_post_meta( $postId, 'open-sun', true ); $open_mon = get_post_meta( $postId, 'open-mon', true ); $open_tue = get_post_meta( $postId, 'open-tue', true ); $open_wed = get_post_meta( $postId, 'open-wed', true ); $open_thu = get_post_meta( $postId, 'open-thu', true ); $open_fri = get_post_meta( $postId, 'open-fri', true ); $open_sat = get_post_meta( $postId, 'open-sat', true ); if(!empty($open_sun)){ $sunday_appear[] = array( '@type' => 'openingHoursSpecification', 'dayofWeek' => "Sunday", 'opens' => get_post_meta( $postId, 'open-sun', true ), 'closes' => get_post_meta( $postId, 'close-sun', true ) ); }else if(!empty($open_mon)){ $monday_appear[] = array( '@type' => 'openingHoursSpecification', 'dayofWeek' => "Monday", 'opens' => get_post_meta( $postId, 'open-mon', true ), 'closes' => get_post_meta( $postId, 'close-mon', true ) ); }else if(!empty($open_tue)){ $tuesday_appear[] = array( '@type' => 'openingHoursSpecification', 'dayofWeek' => "Tuesday", 'opens' => get_post_meta( $postId, 'open-tue', true ), 'closes' => get_post_meta( $postId, 'close-tue', true ) ); }else if(!empty($open_wed)){ $wednesday_appear[] = array( '@type' => 'openingHoursSpecification', 'dayofWeek' => "Wednesday", 'opens' => get_post_meta( $postId, 'open-wed', true ), 'closes' => get_post_meta( $postId, 'close-wed', true ) ); }else if(!empty($open_thu)){ $thursday_appear[] = array( '@type' => 'openingHoursSpecification', 'dayofWeek' => "Thursday", 'opens' => get_post_meta( $postId, 'open-thu', true ), 'closes' => get_post_meta( $postId, 'close-thu', true ) ); }else if(!empty($open_fri)){ $friday_appear[] = array( '@type' => 'openingHoursSpecification', 'dayofWeek' => "Friday", 'opens' => get_post_meta( $postId, 'open-fri', true ), 'closes' => get_post_meta( $postId, 'close-fri', true ) ); }else if(!empty($open_sat)){ $saturday_appear[] = array( '@type' => 'openingHoursSpecification', 'dayofWeek' => "Saturday", 'opens' => get_post_meta( $postId, 'open-sat', true ), 'closes' => get_post_meta( $postId, 'close-sat', true ) ); } add_filter( 'site-reviews/schema/LocalBusiness', function( $schema ) { $postId = get_the_ID(); $schema['@type'] = get_post_meta( $postId, 'business-type', true ); $schema['description'] = get_post_meta( $postId, 'biz-about', true ); $schema['openingHoursSpecification'] = [ $sunday_appear, $monday_appear, $tuesday_appear, $wednesday_appear, $thursday_appear, $friday_appear, $saturday_appear, ]; return $schema; });
But I just get a null value for the openingHoursSpecification fields
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘If Statements inside of an array’ is closed to new replies.