How to retrieve post image url?
-
I’m following an older WordPress Theme lesson and the instructor has created a function that will either grab the ‘featured image’ or ‘post image’ url, depending on which is loaded It’s working for him but not me and my code is exactly the same. I’m thinking this is due to the fact that I am using Gutenberg and he is using the classic editor? Can’t figure this out.
Here’s my code. It displays a featured image correctly but I get this output if I use an image block instead ( string(0) “” )
function sdd_get_attachment() { $output = ''; if ( has_post_thumbnail() ) : $output = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ); else : $attachments = get_posts( array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => get_the_ID() )); if ( $attachments ) : foreach ( $attachments as $attachment ) : $output = wp_get_attachment_url( $attachment->ID ); endforeach; endif; endif; return $output; }
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘How to retrieve post image url?’ is closed to new replies.