我有个问题。
我使用以下代码输出wordpress帖子的图像URL。图像URL与;附加图像“;一篇wordpress文章,而不是文章中实际使用的内容。
如果您在2年前向帖子中添加了图像,但当前不再使用该图像,则旧的未使用图像Url仍将通过代码输出。我想避免这种情况。
我需要做些什么来调整代码,以便只输出那些真正在文章源代码中或在文章中实际使用的图像URL?也可以是附加到其他wordpress帖子的图像。
<?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.\'"/>\';
}
}
}
?>