問題描述
任何人都知道要註銷自定義帖子類型的方法?
有相當於 register_post_type()嗎?
最佳解決方案
目前沒有一個取消註冊一個職位類型的功能,但是這個過程很簡單。
Andrew Nacin 提供了一些關於 trac,found here 的代碼,並在下面發佈。
if ( ! function_exists( 'unregister_post_type' ) ) :
function unregister_post_type( $post_type ) {
global $wp_post_types;
if ( isset( $wp_post_types[ $post_type ] ) ) {
unset( $wp_post_types[ $post_type ] );
return true;
}
return false;
}
endif;
取消註冊 built-in 帖子類型將對 WordPress 有未知的影響,因此請自行承擔風險。取消註冊自定義帖子類型應該是完全安全的,但是您的安裝自然不會清除 (即,註銷一個帖子類型不等同於數據庫中的數據刪除) 。
我可以想象一些這樣可能需要的場景,但是更合理的方法 (如果可能的話),如果不想要的話,首先不要註冊該帖子類型。
次佳解決方案
從 WordPress 4.5 起有這樣做的功能,unregister_post_type 。例:-
function delete_post_type(){
unregister_post_type( 'blocks' );
}
add_action('init','delete_post_type');
第三種解決方案
這對我來説是有效的,像 Rarst 説如果可能的話使用 remove_action()。
add_action( 'after_setup_theme','remove_foundation_options', 100 );
function remove_foundation_options() {
remove_action( 'init', 'Orbit');
}
第四種方案
t31os 注意到,從全局變量中刪除帖子類型是很容易的。
但是,如果你的意思是 non-core post 類型,那麼最好是查找註冊它的代碼,並用 remove_action()解開 (如果它是體面的代碼,那麼它應該被鈎住而不是直接運行) 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。