問題描述

關於影像的另一個問題。

我可以新增/附加影像釋出,而不新增它釋出?這樣做的原因是我可以使用 API​​來操縱它。

最佳解決方案

有一個稱為附件的外掛 http://wordpress.org/extend/plugins/attachments/也許這是你要找的東西。

次佳解決方案

是的,絕對有可能:我在我的一個主題中做到這一點。

您只需將影像新增到帖子中,就像您要將其插入到帖子中,然後只需單擊儲存所有更改,而不要實際單擊 「插入到郵件」 按鈕。

然後,您可以使用以下方式訪問該帖子的相簿圖片:

$images = get_gallery_images();

我在 functions.php 中定義了這個函式:

// get all of the images attached to the current post
    function get_gallery_images() {
        global $post;
        $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
        $galleryimages = array();
        if ($photos) {
            foreach ($photos as $photo) {
                // get the correct image html for the selected size
                $galleryimages[] = wp_get_attachment_url($photo->ID);
            }
        }
        return $galleryimages;
    }

然後在模板檔案中做任何你想要的影像。 (在我的情況下,我迴圈遍歷影像並將它們放在一個 jQuery 滑塊中) 。

還有可以使用的外掛,但是如果可以幫助它,它最好儘量減少外掛。

第三種解決方案

是的你可以。

如果您使用媒體上傳器在帖子的編輯螢幕上上傳影像,或使用 update_post()將附件的 post_parent 欄位設定為要附加到該帖子的 ID,則與該帖子相關聯,無論是否實際上插入了該帖子的內容。

您可以透過呼叫 get_children()(see the codex for examples) 檢索附加到特定帖子的所有影像。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。