簡體   English   中英

取代Wordpress圖片庫

[英]Replacing a Wordpress Image gallery

由於我的上一個問題很快得到了回答,所以我想我會再次嘗試運氣。

我正在嘗試在自己創建的自定義帖子類型中創建畫廊。 我希望能夠通過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. 我對數組有些困惑,所以我將如何選擇數組中的第一個圖像並將其包裝在“ image-large”類的div中,然后將第二個圖像包裝在具有“ image-class”的div中-介質”?

  2. 如何用新的圖庫/圖像替換通過編輯器添加的圖庫/圖像? 現在,我得到了兩個圖像實例,一個是通過編輯器添加的,另一個是通過函數獲得的。

編輯

我想出了問題1。 閱讀關聯數組,意識到您可以執行echo $gallery['src'][0]; 獲取每個圖像的源URL。 仍然對問題2感到困惑。

弄清楚了。

//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' );

這樣就刪除了頁面上的所有圖庫。 但是由於從技術上講我的新畫廊並不是畫廊,所以它就一個人呆了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM