Support » Plugin: Site Reviews » If Statements inside of an array

  • Resolved ekaboom

    (@ekaboom)


    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)
  • Plugin Author Gemini Labs

    (@geminilabs)

    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'] = [];
        $days = [
            'sun' => 'Sunday',
            'mon' => 'Monday',
            'tue' => 'Tuesday',
            'wed' => 'Wednesday',
            'thu' => 'Thursday',
            'fri' => 'Friday',
            'sat' => 'Saturday',
        ];
        foreach ($days as $day => $dayofWeek) {
            $opens = get_post_meta($postId, 'open-'.$day, true);
            if (empty($opens)) {
                continue;
            }
            $schema['openingHoursSpecification'][] = [
                '@type' => 'openingHoursSpecification',
                'dayofWeek' => $dayofWeek,
                'opens' => $opens,
                'closes' => get_post_meta($postId, 'close-'.$day, true),
            ];
        }
        return $schema;
    });
    Plugin Author Gemini Labs

    (@geminilabs)

    On a different note:

    You also have the option of using a schema plugin. All you would need to do to link the Site Reviews schema to other schemas is to give them both the same @id value.

    For example:

    add_filter('site-reviews/schema/LocalBusiness', function ($schema) {
        $schema['@id'] = get_the_permalink(); // as long as the other schema share the same @id, they will link to the Site Reviews schema
    });

    This worked!

    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'] = [];
        $days = [
            'sun' => 'Sunday',
            'mon' => 'Monday',
            'tue' => 'Tuesday',
            'wed' => 'Wednesday',
            'thu' => 'Thursday',
            'fri' => 'Friday',
            'sat' => 'Saturday',
        ];
        foreach ($days as $day => $dayofWeek) {
            $opens = get_post_meta($postId, 'open-'.$day, true);
            if (empty($opens)) {
                continue;
            }
            $schema['openingHoursSpecification'][] = [
                '@type' => 'openingHoursSpecification',
                'dayofWeek' => $dayofWeek,
                'opens' => $opens,
                'closes' => get_post_meta($postId, 'close-'.$day, true),
            ];
        }
        return $schema;
    });

    Thanks so much. I can now stop banging my head against the wall.

    Plugin Author Gemini Labs

    (@geminilabs)

    Great!

    FYI: The reason your previous attempt did not work is because you cannot use PHP variables inside a regular function if they were declared outside…unless you do it like this:

    $name = 'John';
    
    function sayHello() use ($name) {
        printf('Hello %s!', $name);
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.