'Trying to access array offset on value of type bool in /var/www/mysite/wp-content/themes/mytheme/template-parts/single.php
In my blog site everything is working fine but on some posts images are not coming in the image src i am getting this error:
<img class="lazy _single__img" alt="" data-src="<br />
<b>Notice</b>: Trying to access array offset on value of type bool in <b>/var/www/fisharoma/wp-content/themes/wisledge2/template-parts/single.php</b> on line <b>19</b><br />
" src="<br />
<b>Notice</b>: Trying to access array offset on value of type bool in <b>/var/www/fisharoma/wp-content/themes/wisledge2/template-parts/single.php</b> on line <b>20</b><br />
">
According to the error something is wrong in my single.php line 20, but i can't figure out what am doing wrong.
Here is my single.php
<?php
$categories = get_the_category();
if (empty($categories) || !count($categories)) {
return;
}
$term = $categories[0];
?>
<article id="entry-<?php the_ID(); ?>" <?php post_class('_single'); ?>>
<nav class="_path">
<a class="_path__link" href="https://fisharoma.com/">Home ></a>
<a class="_path__link" href="<?php echo get_category_link($term->term_id); ?>"><?php echo $term->cat_name; ?> <?php _e('', 'wisledge'); ?></a>
</nav>
<?php the_title('<h1 class="_single__title">', '</h1>'); ?>
<?php // the_post_thumbnail('large', ['class' => '_single__img']); ?>
<img
class="lazy _single__img"
alt="<?php echo get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true); ?>"
data-src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id(), 'large')[0]; ?>"
src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id(), 'large')[0]; ?>"
/>
<div class="_single__content">
<?php get_template_part('template-parts/share'); ?>
<div class="_meta">
<span class="_meta__by"><?php _e('By ', 'wisledge'); ?></span>
<a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" class="_meta__author"><?php the_author(); ?></a>
<?php if (get_the_time() != get_the_modified_time()) :?>
<span class="_meta__updated"><?php _e('Updated', 'wisledge'); ?></span>
<?php
/*
<!-- <time class="_meta__time" datetime="<?php the_modified_time('c'); ?>"><?php the_modified_time(get_option('date_format')); ?></time> -->
*/
?>
<time class="_meta__time"><?php the_modified_time(get_option('date_format')); ?></time>
<?php endif; ?>
</div>
<?php the_content(); ?>
</div>
</article>
Solution 1:[1]
Try wrapping your image with a check condition first:
if(wp_get_attachment_image_src(get_post_thumbnail_id(), 'large')){
?>
<img
class="lazy _single__img"
alt="<?php echo get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true); ?>"
data-src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id(), 'large')[0]; ?>"
src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id(), 'large')[0]; ?>"
/>
<?php
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eduhud |
