Использование переменной из другого цикла без получения логической ошибкиPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Использование переменной из другого цикла без получения логической ошибки

Сообщение Anonymous »

Итак, у меня есть код для вывода схемы, который работает правильно, но выдает логическую ошибку. «Пытаюсь получить доступ к смещению массива по значению типа 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 '' . wp_json_encode( $address_schema, JSON_PRETTY_PRINT ) . '';
}
}


Подробнее здесь: https://stackoverflow.com/questions/784 ... bool-error
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»