Below each custom post I have two images and a title and excerpt of other posts to read, but the images aren't loading. If I look at the code I see that the image variable is empty.
<?php
foreach( $recent_posts as $recent_post ) {
setup_postdata( $recent_post );
$image_id = get_post_thumbnail_id( $recent_post->ID );
$image_url = '';
if( $image_id > 0 ) {
$image_url = wp_get_attachment_url( $image_id, 'medium' );
}
?>
Should the $image_url
be empty?
Below each custom post I have two images and a title and excerpt of other posts to read, but the images aren't loading. If I look at the code I see that the image variable is empty.
<?php
foreach( $recent_posts as $recent_post ) {
setup_postdata( $recent_post );
$image_id = get_post_thumbnail_id( $recent_post->ID );
$image_url = '';
if( $image_id > 0 ) {
$image_url = wp_get_attachment_url( $image_id, 'medium' );
}
?>
Should the $image_url
be empty?
- 1 I'm not a php programmer but in the code above you are setting image_url to empty, which is fine but then there is an if clause. Is the if clause satisfied so statement after it is executed? – Admiral Noisy Bottom Commented Apr 10, 2020 at 5:31
- The problem is not in the code you posted.. Is the image correctly printed in html..? – Tofandel Commented Apr 10, 2020 at 7:40
- I agree, the problem could be with the conditional check, or anywhere else in your (unposted) code... – Michael Commented Apr 10, 2020 at 17:16
1 Answer
Reset to default 1Most of this code is unnecessary. Remove everything inside the foreach
from your question and just use:
foreach ( $recent_posts as $recent_post ) {
$image = get_the_post_thumbnail( $recent_post, 'medium' );
// etc.
}