Итак, у меня есть код для вывода схемы, который работает правильно, но выдает логическую ошибку. «Пытаюсь получить доступ к смещению массива по значению типа 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
Использование переменной из другого цикла без получения логической ошибки ⇐ Php
Кемеровские программисты php общаются здесь
1715272468
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 ) . '';
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/78455833/using-a-variable-from-another-loop-without-getting-a-bool-error[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия