获取Wordpress循环中插入的帖子媒体文件的链接


get link of inserted media file of post within Wordpress loop

在循环中,我想检索每个帖子的插入媒体文件的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; ?>

但我无法让它发挥作用。我确保每个帖子中都插入了一个文件。此外,我想问一下,如果一个帖子有多个文件,该如何处理。

谢谢!

注意:我指的是插入的文件,而不是特色图片。

    <?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; ?>