2012-01-01 62 views
6

我有一個頁面,其中有一個幻燈片在頂部,圖像插入到內容區域。WordPress的:不包括圖像'插入帖子'from get_children

我需要從幻燈片中排除已插入帖子中的圖像。

目前我不包括'精選圖片',但是這限制了我可以插入到帖子中的一張圖片。

這裏是我現有的代碼:

$thumbnail = get_post_thumbnail_id(); 
$images = get_children('post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); 

此前我已經使用的圖像元數據的描述字段中輸入「排除」,以排除圖像。對於最終用戶來說,這並不像我希望的那樣好。

任何建議,插件或代碼的基礎!

更新: 我已經更新了代碼,所以現在我從POST_CONTENT任何圖像的URL,並檢查他們對幻燈片圖像。

$content = $post->post_content; 
    $inlineImages = array(); 
    preg_match('/src="([^"]*)"/i', $content, $inlineImages) ; 
    $thumbnail = get_post_thumbnail_id($post->ID); 

    $images = get_children('post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); 

    if ($images) { 
     echo '<div id="slideshow">'; 
     foreach ($images as $attachment_id => $attachment) { 
      $image = wp_get_attachment_image_src($attachment_id,array(900,265)); 

      if (!in_array($image[0],$inlineImages)) { 
       echo '<img src="'.$image[0].'" width="'. $image[1] .'" height="'. $image[2].'">'; 
      } 
     } 
     echo '</div>'; 
    } 

這是一個很好的解決方案,雖然正則表達式可以改進。

更好的一步是將圖像陣列添加到自定義字段字段,該字段字段在頁/頁面更新或發佈時更新爲 。

有關如何去解決這個問題的任何建議?

+0

您是否通過管理媒體菜單將幻燈片圖像手動附加到頁面上? – paislee 2012-01-11 01:01:50

回答

4

我已經更新了代碼,所以現在我從post_content獲取任何圖片網址,並根據幻燈片圖片進行檢查。

$content = $post->post_content; 
$inlineImages = array(); 
preg_match('/src="([^"]*)"/i', $content, $inlineImages) ; 
$thumbnail = get_post_thumbnail_id($post->ID); 

$images = get_children('post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); 

if ($images) { 
    echo '<div id="slideshow">'; 
    foreach ($images as $attachment_id => $attachment) { 
     $image = wp_get_attachment_image_src($attachment_id,array(900,265)); 

     if (!in_array($image[0],$inlineImages)) { 
      echo '<img src="'.$image[0].'" width="'. $image[1] .'" height="'. $image[2].'">'; 
     } 
    } 
    echo '</div>'; 
} 
-1

我還沒有完全理解你的問題,但如何排除幻燈片存在的div ID?

$thumbnail = get_post_thumbnail_id(); 
$images = get_children('post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail. '&NAME'); 

將'NAME'替換爲縮略圖後'。在括號內。 希望這有助於。

+0

這是行不通的。 Get_children已經設置了參數。不過謝謝。 – addedlovely 2012-01-04 11:42:18

0

我認爲最簡單的事情將要使用的流星幻燈片插件創建每一頁的幻燈片,然後插入短碼在正確的頁面的內容區域適當的幻燈片。是的,這意味着您必須在頁面編輯器的「外部」編輯每個頁面,但它也使您可以輕鬆完全控制哪些照片在每個幻燈片中都不會出現,並且簡碼很容易與頁面編輯器。

0

Media Custom Fields插件允許您將自定義數據添加到媒體項目,就像使用自定義帖子字段一樣,從而爲用戶提供了標記幻燈片專用圖像的方式。從插件FAQ

除了爲您提供添加自定義字段的選項之外,您的自定義字段將顯示在所有適當的位置。如果你像媒體部分那樣去管理媒體,他們會在編輯媒體表單上顯示。自定義字段也會在您將圖片上傳到帖子以及所有其他預期位置時顯示。

一種方法是安裝這個插件,並與價值TRUE你的幻燈片圖像通過儀表板添加自定義字段,如Slide - >媒體菜單。然後你的過濾器的代碼可能是:

$thumbnail = get_post_thumbnail_id(); 
$images = get_children(/* removed for brevity */); 

if ($images) { 
    // .. 
    foreach ($images as $attachment_id => $attachment) { 
     // skip images not marked for slideshow 
     if (!get_post_meta($attachment_id, 'Slide')) continue; 

     // otherwise do slideshow things 
    } 
    // .. 
} 

注意,這種方法對於Slide值可以設置爲任何東西,除了空字符串。您可能希望將Slide定義爲類別常量以避免硬編碼。

優勢

  1. 不需要在海報上的任何工作
  2. 未來:插件似乎是建立在良好的老後的功能和附件都只是1種無論如何發佈,所以沒有什麼奇怪的。
6

只需要做同樣的事情。您的原創方式就是我想要的方式 - 只需排除插入到滑塊中的任何圖像。但我不希望客戶必須做任何特別的事情才能實現。這是我的代碼。

$args = array('post_type' => 'attachment', 'post_mime_type'=>'image','numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID); 
$attachments = get_posts($args); 
preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $matches, PREG_PATTERN_ORDER); 
/* $matches[1] holds the urls as an array */ 
foreach ($attachments as $attachment) { 
if(in_array($attachment->guid, $matches[1])){ continue;} 
wp_get_attachment_image($attachment->ID , 'slider_size'); 
} 

首先得到與帖子相關的所有圖像。 $ preg_match_all獲取帖子正文中的所有圖像。然後,當我們循環顯示圖像以在滑塊中顯示它們時,in_array會檢查插入圖像的url的URL是否要添加到滑塊,如果匹配則跳到下一個圖像的url。

感謝你的帖子,讓我在正確的方向思考。

+0

不錯的方法!這可能只適用於最近的'WP'版本,但我發現'$ attachment-> guid'和'$ matches''Array'中的值不匹配,因爲插入到帖子中的圖像會附加圖像大小到文件名的末尾?我的快速解決方案是在插入沒有附加文件大小的圖像時選擇「大小」已滿或在文章中進行編輯,這兩種解決方案都不是很好。 – Brownrice 2015-03-02 12:36:00

0

這是另一種方法。

我不喜歡使用網址,因爲插入內容區域內的圖片可能具有不同的大小,如中等,縮略圖或完整。所以,只有url不匹配。

使用上述代碼,

function printMeImages() { 

    $content = $post->post_content; 
    $inlineImages = array(); 

    // populate ids of images from wp-image-id 
    preg_match_all('/wp-image-([^"]*)"/i', $content, $inlineImages) ; 

    $thumbnail = get_post_thumbnail_id($post->ID); 

    $images = get_children('post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail); 

    $out = ""; 

    if ($images) { 
     $out .= '<ul id="slideshow">'; 
     foreach ($images as $attachment_id => $attachment) { 
      $image = wp_get_attachment_image_src($attachment_id,'desiredImageSize'); 
      // $inlineImages[1] has ids to be discarded 
      if (!in_array($attachment_id,$inlineImages[1])) { 
       $out .= '<li><img src="'.$image[0].'" width="'. $image[1] .'" height="'. $image[2].'"></li>'; 
      } 
     } 
     $out .= '</ul>'; 
    } 

    return $out; 

} 

爲精選未設置,而不是交內使用的檢索的圖像。

+0

這可能不起作用,因爲圖像可能會上傳到不同的文件夾,具體取決於您的Permilink結構。 – Relequestual 2014-05-01 09:20:34

0

出於性能原因,我不會在頁面呈現時執行代碼,而是將代碼綁定到save_post鉤子。當文章內容被編輯並保存時,將調用掛鉤,並將所有未在內容中使用的附加圖像(或其ID)保存到postmeta表中。當滑塊呈現時,可以通過get_post_meta($ post_id,'image_ids_for_slider')訪問圖像ID。這樣我就可以避免在頁面渲染時執行preg_match_all操作。當帖子內容很小並且只有一個滑塊加載時,這可能不是性能原因,但通常我會發現這種方法因縮放比較乾淨。

//hook this function to save post action 
function save_ids_of_image_attachments_not_used_in_the_content($post_id, $post) { 

    $args = array( 'post_type' => 'attachment', 
        'post_mime_type'=>'image', 
        'numberposts' => -1, 
        'post_status' => null, 
        'post_parent' => $post_id 
    ); 
    $attachments = get_posts($args);  
    preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $matches, PREG_PATTERN_ORDER); 

    $ids_of_attached_images_not_in_content = array(); 

    /* $matches[1] holds the urls as an array */ 
    foreach ($attachments as $attachment) { 
     if(in_array($attachment->guid, $matches[1])){ 
      continue; 
     } 
     $ids_of_attached_images_not_in_content[] = $attachment->ID; 
    } 

    // save the image_ids as postmeta 
    update_post_meta($post_id, 'image_ids_for_slider', $ids_of_attached_images_not_in_content); 
} 
add_action('save_post', 'save_ids_of_image_attachments_not_used_in_the_content', 10, 2);