2011-03-17 24 views
2

我在single.php中使用了一個簡單的jQuery圖像幻燈片,即將所有圖像附件調用到我的文章。但我想排除在實際文章中張貼的圖像,但保留實際附加到文章的所有圖像。從附件圖像中排除帖子中的圖像

我使用這段代碼抓住所有的圖片附件,但不幸的是,它也抓住了文章的內容,這是沒有必要的範圍內的所有圖片:

<?php 
$attachs = get_posts(array(
    'numberposts' => -1, 
    'post_type' => 'attachment', 
    'post_parent' => get_the_ID(), 
    'post_mime_type' => 'image', // get attached images only 
    'output' => ARRAY_A 
)); 

if (!empty($attachs)) { 
    foreach ($attachs as $att) { 
     // get image's source based on size, 
     // can be 'thumbnail', 'medium', 'large', 'full' 
     // or registed post thumbnails sizes 
     $src = wp_get_attachment_image_src($att->ID, 'full'); 
     $src = $src[0]; 

     // show image 
     echo "<div style='margin: 0 10px 10px 0; float: left'><img src='$src' /></div>"; 
    } 
} 
?> 

任何建議?

+0

您是否找到了解決方案? – testing 2012-03-07 17:32:16

回答

0

在WordPress中,不幸的是,插入到帖子中的附加圖像和圖像沒有區別。人們在網絡上使用的常見解決方案是在帖子中添加一個meta_data,例如「_inserted_image」(true | false),並在插入並在帖子中內聯圖像時使用掛鉤來更新此值。在我看來,這太複雜了。

一個簡單的解決方法是專門創建一個類別你的幻燈片,並把用於幻燈片的所有圖片來自媒體庫此類別中的一次。然後,只需在您的帖子參數中添加'category'=>'for_slideshows'即可。

$attachs = get_posts(array(
       'numberposts' => -1, 
       'post_type' => 'attachment', 
       'post_parent' => get_the_ID(), 
       'post_mime_type' => 'image', // get attached images only 
       'output' => ARRAY_A, 
       'category' => 'for_slideshows' 
       )); 

希望它有幫助。

+0

'_inserted-image'解決方案:http://wordpress.stackexchange.com/a/53362/12615 – brasofilo 2012-11-19 14:49:20