2014-10-09 64 views
0

在循环内我想检索每个帖子的插入媒体文件的URL。我的尝试是:在Wordpress循环中获取帖子插入媒体文件的链接

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

*插入媒体*您的意思是精选图像吗? – 2014-10-09 11:41:57

回答

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

这只显示1个帖子,没有循环通过每个帖子。 – supersize 2014-10-09 12:56:18

+0

哎呀!改变上面代码中的numberposts值以获得所需的帖子数量(如果你想让所有的媒体改为-1,我已经将它改为99):D – Yamu 2014-10-10 06:15:14

相关问题