2014-10-27 49 views
0

由於我最後一個問題的答案很快,我以爲我會再次嘗試我的運氣。替換Wordpress圖片庫

我想在我創建的自定義帖子類型內創建一個畫廊。我希望能夠通過wordpress管理編輯器將圖像/圖庫添加到帖子中,但是然後有一個功能將圖像拖動到div中,並用新圖像替換現有圖庫。

我想這樣做,因爲我希望圖像適合不同大小的圖像網格。例如,圖像1將是全寬,圖像2將是寬度的一半,圖像3是四分之一等等。

我已經嘗試了兩種方法,一種是get_children()

$featuredImage = get_post_thumbnail_id($post->ID); 
$imageArgs = array(
    'numberposts' => 5, 
    'order' => 'DESC', 
    'post_mime_type' => 'image', 
    'post_parent' => $post->ID, 
    'post_type' => 'attachment', 
    'exclude' => $featuredImage 
    ); 
$attachments = get_children($imageArgs, ARRAY_A); 
$rekeyed_array = array_values($attachments); 
$child_image = $rekeyed_array[0]; 
echo '<div class="project-img"><img src="' . $child_image['guid'] . '" class="project-image"></div>'; 
$child_image = $rekeyed_array[1]; 
echo '<div class="project-img w2"><img src="' . $child_image['guid'] . '"></div>'; 
$child_image = $rekeyed_array[2]; 
echo '<div class="project-img w3"><img src="' . $child_image['guid'] . '"></div>'; 
echo '<div class="project-img w3"><img src="' . $child_image['guid'] . '"></div>'; 

,另一個是get_post_gallery()

$gallery = get_post_gallery(get_the_ID(), false); 

      /* Loop through all the image and output them one by one */ 
      foreach($gallery['src'] AS $src) 
      { 
       ?> 
       <div class="project-img"> 
       <img src="<?php echo $src; ?>" alt="Gallery image" /> 
       </div> 
       <?php 
      } 

我還沒有取得很大進展與get_post_gallery(),但我八九不離十明白,​​我會必須使用wp_get_attachment_url()才能獲得全尺寸的圖像,而不是縮略圖。現在

,有兩個問題:

  1. 我有點困惑陣列,所以我怎麼會去 數組中選擇第一圖像和與 類包裝它在一個DIV「圖像 - 「放大」,然後第二個圖像,幷包裝在 div類「圖像中」?

  2. 如何用新圖庫/圖像替換通過編輯器 添加的圖庫/圖像?現在,我通過編輯器添加了兩個 圖像實例,原始圖像以及通過函數獲取的圖像 。

編輯

我想通了問題1,我想。閱讀關聯數組,並意識到您可以執行諸如echo $gallery['src'][0];之類的操作來獲取每個圖像的源網址。不過,仍然困惑於問題2。

回答

0

想通了。

//Remove original Gallery 
function remove_the_first_gallery($output, $attr){ 
    $output = '<!-- gallery 1 was here -->'; // Must be non-empty. 
    return $output; 
} 

add_filter('post_gallery', 'remove_the_first_gallery'); 

刪除頁面上的所有圖庫。但是因爲我的新畫廊在技術上並不是一個post_gallery,所以它一個人留下。