Default Featured Image

外掛說明

這個外掛可以協助網站管理員在 [工具]→[媒體] 頁面中設定預設精選圖片,當作者沒有為內容設定精選圖片時,便會顯示預設的精選圖片,簡單有效。

請先查看常見問題集了解常見問題的答案。
如有需要,可以隨時在技術支援論壇或外掛的 GitHub 存放庫與我聯絡。

螢幕擷圖

  • [媒體] 頁面的外掛設定
  • 在 [選取預設精選圖片] 中設定預設精選圖片

安裝方式

  1. 將解壓縮得到的 default-featured-image 資料夾上傳至網站的 /wp-content/plugins/ 目錄。
  2. 在 WordPress 管理後台的 [外掛] 選單中啟用外掛。
  3. 前往 [設定]→[媒體] 選取圖片。

常見問題集

選取的預設精選圖片並未顯示,外掛無法正確執行的原因為何?

這個外掛在大多數的情況下,安裝啟用後便立即可用。如果外掛無法執行,請嘗試以下操作。

  • 切換佈景主題。有些佈景主題會有非常規的執行方式。
  • 檢查網站是否使用 WordPress 核心程式功能取得圖片 (請參考下一條)。
  • 檢查一般精選圖片是否正常。
  • 檢查是否透過 CSS 隱藏精選圖片?Default featured image 可使用 default-featured-img 類別達成這個目的。

如果仍然發生問題,請與我聯絡,讓我了解問題的發生原因。

哪些是網站管理員可以用來顯示精選圖片的函式?

這個外掛並未使用新的函式,全部使用 WordPress 核心程式內建的函式。

是否能為自訂內容類型設定不同圖片?

可以。下列程式碼片段能讓每個自訂內容類型設定不同的預設精選圖片。

add_filter( 'dfi_thumbnail_id', 'dfi_posttype_book', 10, 2 );
function dfi_posttype_book( $dfi_id, $post_id ) {
    $post = get_post( $post_id );
    if ( 'book' === $post->post_type ) {
        return 31; // the image id for the book post type.
    }

    return $dfi_id; // the original featured image id.
}

是否能為每個分類設定不同圖片?

可以。下列程式碼片段能讓每個分類設定不同的預設精選圖片。

add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 );
function dfi_category( $dfi_id, $post_id ) {
    // Set a different image for posts that have the 'cats' category set.
    // This will trigger first, if multiple categories have been set.
    if ( has_category( 'cats', $post_id ) ) {
        return 7; // cats img id.
    }
    // Set a different image for posts that have the 'cats' category set.
    if ( has_category( 'dogs', $post_id ) ) {
        return 8; // dogs img id.
    }

    return $dfi_id; // the original featured image id.
}

是否能變更預設精選圖片的 HTML 程式碼?

啟用這個外掛後,它會新增額外的 default-featured-img 類別,這個類別可用於進行樣式化。

如果需要變更更多項目,可以使用 dfi_thumbnail_html 篩選器變更全部 HTML 程式碼。

add_filter( 'dfi_thumbnail_html', 'dfi_add_class', 10, 5 );
function dfi_add_class( $html, $post_id, $default_thumbnail_id, $size, $attr ) {
    // Add a class to the existing class list.
    $attr['class'] .= ' my-class';

    return wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
}

是否能讓某個頁面不使用預設的精選圖片?

下列程式碼片段能讓 ID 為 23 的文章/頁面不使用預設的精選圖片。

add_filter( 'dfi_thumbnail_id', 'dfi_skip_page', 10, 2 );
function dfi_skip_page( $dfi_id, $post_id ) {
    if ( $post_id == 23 ) {
        return 0; // invalid id.
    }

    return $dfi_id; // the original featured image id.
}

使用者評論

2024 年 10 月 12 日
Thank you, @janwoostendorp, for this great minimalist and out-of-the-box working plugin. I was looking for a plugin that’d set a default image to posts that don’t have any, and this plugin is doing exactly that—nothing more, nothing less. It’s not creating yet another admin menu bar item with dozens of subpages. It doesn’t annoy you with constant dashboard notices. It doesn’t beg for donations or includes any watermarks. It just works the way you expect it to work. It’s a little sad that the bar is this low, but compared to so many other plugins, I think this needs to be pointed out—and reach more people who want a simple solution for a simple problem. The only feature I’d have liked to see is the support for images that are unavailable and can’t be displayed to also use the default featured image. A quick glance inside the code showed quite a bit of jQuery, which wouldn’t be my first choice, but it made me think that adding a simple checkbox to have it checking for this (non-render blocking, of course) could be a useful idea. Thanks for your work!
2024 年 3 月 14 日
Default featured image does what it’s supposed to do and it meets two criteria I always look for: light weight it just works The support forum has good answers to questions, and that’s a big plus.Thanks for the great plugin.
2024 年 1 月 12 日 1 則留言
Es perfecto porque hay una opción que te pone automáticamente la imagen destacada de todos los post que no la tenían cogiendo la primera imagen del post y si no hay imagen lo deja vacío. Perfecto!!
閱讀全部 64 則使用者評論

參與者及開發者

以下人員參與了開源軟體〈Default Featured Image〉的開發相關工作。

參與者

〈Default Featured Image〉外掛目前已有 12 個本地化語言版本。 感謝全部譯者為這個外掛做出的貢獻。

將〈Default Featured Image〉外掛本地化為台灣繁體中文版

對開發相關資訊感興趣?

任何人均可瀏覽程式碼、查看 SVN 存放庫,或透過 RSS 訂閱開發記錄

變更記錄

1.7.3

  • PHP 7.4 and WP 6.2 are now required. This is to use the new WP_HTML_Tag_Processor functions.
  • Fixed a bug where classes were overridden.

1.7.2.1

  • Development is now done in git.

1.7.2

  • Added extra context to a translation as suggested by Alex Lion

1.7.1

  • Fixed weird SVN deployment bug.

1.7.0

  • moved main class to it’s own file.
  • Added second class that can hold exceptions with other plugins
  • The first exception is for WP User Frontend
  • The second exception is for WP All Import.

1.6.4

  • get_post_meta($post_id) without specifying the meta_key didn’t find the DFI. It will now even use an even deeper level and set it in the core cache.

1.6.3

  • Fixed plugin header which blocked installing it.

1.6.2

  • Plugin now follows WP coding standard
  • Fixed a small bug where DFI overrides attachments featured images. mp3 has a music note by default, DFI should not override that.

1.6.1

  • Small readme.txt update.

1.6

  • On of the last fixes didn’t account for all situations.

1.5

  • Fixed two small (and rare) warnings
  • Added translation domain

1.4

  • Added plugin images both the plugin header as the thumbnail. Based on the boat WP.org uses in it’s theme previews
  • Fixed a bug where the ajax calls didn’t return the DFI forum thread

1.3

  • Filter dfi_thumbnail_id now also returns the post ID of the post (or any postype) that is being called. See the FAQ for new examples

1.2

  • Filter dfi_thumbnail_id is now called in an earlier stage.

1.1

  • Fixed inheriting classes of the image

1.0

  • Plugin will now remove it’s setting on plugin removal
  • added a default class to the <img> tag, if it shows a default featured image
  • The default featured image will now also return with get_post_thumbnail_id, making the chance that it fail far far smaller.
  • The image given in the media page is now validated

0.9

  • Launch