Written by Kasumi

WordPressのタグ選択をチェックボックスに変更する方法

WordPress投稿記事のタグを選択時、フリーワードで追加する仕様になってるが、カテゴリーと同じようにチェックボックスで選ぶ仕様に変更したい。

本記事ではこのような悩みを解決、実装方法について解説します。

functions.phpにコードを記述

//-----------------------------------------------------
// 【タグをチェックボックスで選択】
//-----------------------------------------------------
function post_tag_checkbox() {
  global $wp_rewrite;
  $rewrite = array(
    'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
    'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
    'ep_mask' => EP_TAGS,
  );
  $labels = array(
    'name' => _x( 'Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' => __( 'Search Tags' ),
    'popular_items' => __( 'Popular Tags' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Tag' ),
    'view_item' => __( 'View Tag' ),
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
    'separate_items_with_commas' => __( 'Separate tags with commas' ),
    'add_or_remove_items' => __( 'Add or remove tags' ),
    'choose_from_most_used' => __( 'Choose from the most used tags' ),
    'not_found' => __( 'No tags found.' )
  );
  register_taxonomy( 'post_tag', 'post', array(
    'hierarchical' => true,
    'query_var' => 'tag',
    'rewrite' => $rewrite,
    'public' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    '_builtin' => true,
    'labels' => $labels
  ));
}
add_action( 'init', 'post_tag_checkbox', 1 );

上記コードをfunctions.phpに追記する事でチェックボックス仕様に変更できます。

以上で実装完了です。

まとめ

デフォルト仕様だとフリーワードで入力したテキストが自動的に新規でタグ追加されます。

便利な機能ですが、スラッグ(URL)なども自動で設定したタグ名に変更されます。

カテゴリと同じくチェックボックスであらかじめ設定されたタグ一覧から選びたい場合は本記事を参考に実装してみてください。

以上で解説を終わります。

目次

関連記事

WordPress プログラミング

【簡単】wordpressで共通コードをパーツ化させる方法【プラグイン不要】

更新日:2022.04.07
2494
WordPress

【簡単】WordPressで編集するテンプレートファイルを一目で確認する方法

2022.09.07
690
WordPress プログラミング

【BreadcrumbNavXT】パンくずをJSONLD形式構造化で表示する方法

2022.01.02
2273
WordPress プログラミング

【ワードプレス】親子カテゴリ・親子ターム順にリンクリストを自動で表示する方法

更新日:2022.05.31
3427