2014-10-09 42 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

相關問題