php – Attempt to read property “ID” on null PHP8 warning

Question

I’m trying to resolve some PHP warnings that fill up the server logs. They are

Attempt to read property "ID" on null in functions.php on line 821
Undefined variable $loop in functions.php on line 821
Attempt to read property "post" on null in functions.php on line 821

This is the code in question; it creates some variables from the post data which are passed to Javascript. When I change $loop->post->ID to $post->ID, I still get the other two errors.

This is running PHP8.

Any ideas?

if ( is_product() ) { 
    
global $product; ?>

<script>
.....
</script>

<?php

$ImageUrl = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 
'single-post-thumbnail' )[0];   // Line 821
$ItemId = $product->id;
$Title = $product->name;
$ProductUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$CurrencySymbol = get_woocommerce_currency_symbol();
$Currency = get_woocommerce_currency();
$Price = $product->price;
$RegularPrice = $product->regular_price;
$DiscountAmount = (int)$RegularPrice - (int)$Price;
$terms = get_terms( 'product_tag' );

0
BlueDogRanch 2 months 2021-08-09T10:10:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse