本文や抜粋などが不要なケースどうするか?

カスタマイズ案件で、入力項目をほぼカスタムフィールドで実装するケースがあって、入力順的に本文や抜粋が邪魔だなと思ったときに削除(非表示)にする方法を調べていた時のメモ。

投稿の本文を削除(非表示)

投稿の本文を削除(非表示)する場合はこんな感じ。

function remove_support() {
  remove_post_type_support( 'post', 'editor' );
}
add_action( 'init' , 'remove_support' );

remove_post_type_support()関数を使用します。便利。第一引数は投稿タイプなので、投稿だけでなく固定ページ(page)だったり、カスタム投稿タイプでもいける模様。

第二引数が削除(非表示)にしたい機能で、マニュアルだと以下のパラメータが渡せる模様。

  • ‘title’
  • ‘editor’ (content)
  • ‘author’
  • ‘thumbnail’ (featured image) (current theme must also support Post Thumbnails)
  • ‘excerpt’
  • ‘trackbacks’
  • ‘custom-fields’
  • ‘comments’ (also will see comment count balloon on edit screen)
  • ‘revisions’ (will store revisions)
  • ‘page-attributes’ (template and menu order) (hierarchical must be true)
  • ‘post-formats’ removes post formats, see Post Formats

今回は、本文と抜粋を削除(非表示)にするので、editorexcerptを指定します。

function remove_support() {
  remove_post_type_support( 'post', 'editor' );
  remove_post_type_support( 'post', 'excerpt' );
}
add_action( 'init' , 'remove_support' );

できた!\(^o^)/
第二引数の型は文字列(string)なので、一つずつ指定しないといけないのか。配列にしてもいい気がする今日この頃。

カテゴリーとタグを削除(非表示)

カテゴリーとタグは別の関数を使用します。unregister_taxonomy_for_object_type()という関数でタクソノミーと投稿タイプを指定して削除(非表示)にします。

function remove_support() {
  unregister_taxonomy_for_object_type( 'category', 'post' );
  unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}
add_action( 'init' , 'remove_support' );

できた!\(^o^)/
こちらも一つずつ指定しないといけない模様。

参考サイト

Developer Resources: remove_post_type_support()
https://developer.wordpress.org/reference/functions/remove_post_type_support/
Developer Resources: unregister_taxonomy_for_object_type()
https://developer.wordpress.org/reference/functions/unregister_taxonomy_for_object_type/