How to echo images Urls from a wordpress post, that are relally in the post
I have a question.
I use the following code to output the image URLs of a wordpress post. The image URLs are related to the attached images of a wordpress post and not the ones that are actually used in the post.
If you added an image to a post 2 years ago, but currently don't use the image anymore, the old unused Image Url will still be output via the code. I would like to avoid this.
What i need to do to adjust the code to output only those Image URLs that are really in the source code of the post or really actually used in the post? That can be also Images that are attached to another wordpress post.
?php
if( is_single() ) {
global $post;
$args = array( 'post_type' = 'attachment', 'numberposts' = -1, 'post_mime_type' = 'image', 'post_status' = null, 'post_parent' = $post-ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$img_title = $attachment-post_excerpt;
$metadata = wp_get_attachment_metadata($attachment-ID);
$width = $metadata['width'];
$height = $metadata['height'];
echo 'meta property=og:image content='.$attachment-guid.'/';
echo 'meta property=og:image:type content=image/jpeg /';
echo 'meta property=og:image:width content='.$width.'/';
echo 'meta property=og:image:height content='.$height.'/';
}
}
}
?
Topic header-image post-thumbnails attachments images Wordpress
Category Web