在循环内我想检索每个帖子的插入媒体文件的URL。我的尝试是:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href="<?php wp_get_attachment_url(the_ID()) ?>">
<?php the_title(); ?>
</a>
<?php endwhile; ?>
<?php endif; ?>
但我无法让它发挥作用。我确保每个帖子都插入了一个文件。此外,我想问一下,如果帖子有多个文件,如何处理。
谢谢!
注意:我的意思是插入的文件,而不是特色图片。
答案 0 :(得分:0)
<?php if (have_posts()) : while (have_posts()) : the_post();
if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 99,// -1 to get all images
'post_status' => null,
'post_parent' => $post->ID
)));
//the $attachments will have all the images/media attached or used in your post. You can loop through it an use the data as required.
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , true, false, 'Link to image attachment' );
}
?>
<?php endwhile; ?>
<?php endif; ?>