使用另一个循环中的变量而不会出现布尔错误

问题描述 投票:0回答:1

所以我有这个用于输出模式的代码,它可以正常工作,但它给了我一个布尔错误。 “尝试访问 bool 类型值的数组偏移量”

我从页面 $map_location 上的另一个循环获取地址变量,并将子变量添加到 $contact_numbers 中。因为它们共享相同的内容。

有没有相对简单的方法来解决这个问题?

    /**
     * 'wp_head' callback
     * Insert structured data into head
     */
    public function add_address_schema() {
        // Return if not hospital post.
        if ( get_post_type() !== 'location' ) {
            return;
        }

        global $luna;
        $id_post         = get_the_id();
        $map_location    = get_field( 'map_location', $id_post );
        $contact_numbers = get_field( 'contact_numbers', $id_post );
        $thumbnail_id    = get_post_thumbnail_id( $id_post );
        $location_type   = $luna->utils->get_the_primary_term( 'location-type', $id_post );

        $address_schema = [
            '@context' => 'https://schema.org',
            '@type'    => $location_type ? $location_type->name : __( 'Hospital', 'luna' ),
            'name'     => get_the_title( $id_post ),
            'url'      => get_the_permalink( $id_post ),
        ];
        
        if ( $thumbnail_id ) {
            $address_schema['image'] = get_the_post_thumbnail_url( $id_post, 'large' );
        }
        
        if ( $map_location ) {
            $address_schema['address'] = [
                '@type'           => 'PostalAddress',
                'streetAddress'   => $map_location['address'],
                'addressLocality' => $map_location['city'],
                'addressRegion'   => $map_location['state'],
                'postalCode'      => $map_location['post_code'],
                'addressCountry'  => $map_location['country_short'],
            ];
        
            $address_schema['geo'] = [
                '@type'     => 'GeoCoordinates',
                'latitude'  => $map_location['lat'],
                'longitude' => $map_location['lng'],
            ];
        }
        
        if ( $contact_numbers && is_array( $contact_numbers ) ) {
            $address_schema['department'] = [];
            foreach ( $contact_numbers as $contact ) {
                $address_schema['department'][] = [
                    '@type'     => $location_type ? $location_type->name : __( 'Hospital', 'luna' ),
                    'name'      => $contact['label'],
                    'telephone' => $contact['number'],
                    '@type'           => 'PostalAddress',
                'streetAddress'   => $map_location['address'],
                'addressLocality' => $map_location['city'],
                'addressRegion'   => $map_location['state'],
                'postalCode'      => $map_location['post_code'],
                'addressCountry'  => $map_location['country_short'],
                ];
                
                
            }
            
        }

        echo '<!-- Auto generated address structured data --><script type="application/ld+json">' . wp_json_encode( $address_schema, JSON_PRETTY_PRINT ) . '</script>';
    }
}
php json wordpress
1个回答
0
投票
        if ( $contact_numbers && is_array( $contact_numbers ) ) {
        $address_schema['department'] = [];
        foreach ( $contact_numbers as $contact ) {
            $address_schema['department'][] = [
                '@type'     => $location_type ? $location_type->name : __( 'Hospital', 'luna' ),
                'name'      => $contact['label'],
                'telephone' => $contact['number'],
                ];
                if ( $map_location ) {
                $address_schema['address'] = [
            
            'streetAddress'   => $map_location['address'],
            'addressLocality' => $map_location['city'],
            'addressRegion'   => $map_location['state'],
            'postalCode'      => $map_location['post_code'],
            'addressCountry'  => $map_location['country_short'],
            ];
                }
            
        }
        
    }
© www.soinside.com 2019 - 2024. All rights reserved.