PHP Warning: Attempt to read property “post_content” on null
-
Recently I upgraded my PHP version from 7.4 to 8.1.
Now I’m getting error_log as given below:
PHP Warning: Attempt to read property "post_content" on null in /some-path/wp-content/themes/boldr-lite/functions.php on line 267
PHP Warning: Attempt to read property "post_content" on null in /some-path/wp-content/themes/boldr-lite/functions.php on line 268
I checked the functions.php and line number 267 and 268 as given below:
preg_match( '/<!--more(.*?)?-->/', $post->post_content )
|| preg_match( '/<!--nextpage-->/', $post->post_content )
And the full function including the above two lines as given below:
/* * Rewrite and replace wp_trim_excerpt() so it adds a relevant read more link * when the <!--more--> or <!--nextpage--> quicktags are used * This new function preserves every features and filters from the original wp_trim_excerpt */ function boldr_trim_excerpt( $text = '' ) { global $post; $raw_excerpt = $text; if ( '' === $text ) { $text = get_the_content( '' ); $text = strip_shortcodes( $text ); $text = apply_filters( 'the_content', $text ); $text = str_replace( ']]>', ']]>', $text ); $excerpt_length = apply_filters( 'excerpt_length', 55 ); $excerpt_more = apply_filters( 'excerpt_more', ' [...]' ); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); /* If the post_content contains a <!--more--> OR a <!--nextpage--> quicktag * AND the more link has not been added already * then we add it now */ if ( ( preg_match( '/<!--more(.*?)?-->/', $post->post_content ) || preg_match( '/<!--nextpage-->/', $post->post_content ) ) && strpos( $text, $excerpt_more ) === false ) : $text .= $excerpt_more; endif; } return apply_filters( 'boldr_trim_excerpt', $text, $raw_excerpt ); } remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' ); add_filter( 'get_the_excerpt', 'boldr_trim_excerpt' );
Please help me to solve this issue, suggest me a corrected code
- The topic ‘PHP Warning: Attempt to read property “post_content” on null’ is closed to new replies.