問題描述
我已經創建了一個自定義的 post 類型作為插件,並將其發佈到存儲庫中。其中一個核心功能包括使用特色圖像。我已經在 register_post_type()中將 thumbnail 添加到 $supports,所以在 「管理」 面板中顯示了該方框。我也掛着 after_setup_theme,並調用 add_theme_support( 'post-thumbnails' ),但我不認為它會影響。
Codex 説 you have to call it from the theme’s functions.php file,但是如果是真的,那麼只有用户的主題調用 add_theme_support( 'post-thumbnails' )(這將涵蓋所有的帖子類型),如果主題沒有調用它,或者只是在特定的類型上調用它,那麼它就會有效不工作
有沒有人看到這個問題的方法?
最佳解決方案
核心代碼中有評論應該改進,但是已經有一段時間了。基本上沒有本機功能添加或刪除部分功能,僅功能完全。
主題完成後,手動執行此操作 (after_setup_theme 鈎子後):
function add_thumbnails_for_cpt() {
    global $_wp_theme_features;
    if( empty($_wp_theme_features['post-thumbnails']) )
        $_wp_theme_features['post-thumbnails'] = array( array('your-cpt') );
    elseif( true === $_wp_theme_features['post-thumbnails'])
        return;
    elseif( is_array($_wp_theme_features['post-thumbnails'][0]) )
        $_wp_theme_features['post-thumbnails'][0][] = 'your-cpt';
}
次佳解決方案
這是我最後使用的,這是 Rarst 的答案的修改版本
public function addFeaturedImageSupport()
{
    $supportedTypes = get_theme_support( 'post-thumbnails' );
    if( $supportedTypes === false )
        add_theme_support( 'post-thumbnails', array( self::POST_TYPE ) );
    elseif( is_array( $supportedTypes ) )
    {
        $supportedTypes[0][] = self::POST_TYPE;
        add_theme_support( 'post-thumbnails', $supportedTypes[0] );
    }
}
add_action( 'after_setup_theme',    array( $this, 'addFeaturedImageSupport' ), 11 );
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。