のバックストーリー
コミュニティは、WordPress 2.8に戻って以来、用語メタデータを制御する簡単な方法を求めています。それは遅いプロセスでしたが、最終的に用語はクラス構造を使用するためにゼロから再設計されました。これに加えて、WordPress 4.4のいくつかの異なる変更は、分類法(「タグ」、「カテゴリ」、カスタムなど)の用語が独自のメタを簡単に割り当てることができることを意味します。メタデータ操作古い方法
WordPress 4.4の前に、用語のメタデータを簡単に保存する明確な方法がなかったため、これはその構築方法からの用語に固有の制限でした。分類法または用語を延長している場合は、update_optionを使用してサイトオプションとしてデータを直接保存する必要があります。 これは理想的ではありませんでした(オプションテーブルを乱雑にしたため)。
//saving new fields for category function save_extra_taxonomy_fields($term_id){ $term = get_term($term_id); $term_slug = $term->slug; //collect category image id from posted values $term_category_image_id = isset($_POST['category_image_id']) ? sanitize_text_field($_POST['category_image_id']) : ''; //update value and save it as an option update_option('category_image_id_' . $term_slug, $term_category_image_id); } add_action('create_category','save_extra_taxonomy_fields');
上記の例では、create_categoryフックに添付された関数(新しいカテゴリ用語を作成するとトリガー)を実行します。これは私たちの価値を探し、消毒後にオプションとして保存します。これは機能しますが、それほどきれいではありません。
Term Metaを使用するには、add_term_meta、update_term_meta、delete_term_meta関数を使用します。これらの機能により、新しいUI要素と組み合わせると、用語の新しいメタデータを保存および更新できます。
用語にメタデータを追加するには、add_term_meta関数が含まれます。オプションの4番目のパラメーターを指定する必要があります。
function add_featured_to_categories(){ //get all terms from the category taxonomy $taxonomy_name = 'category'; $term_args = array( 'orderby' => 'name', 'hide_empty' => false, 'fields' => 'ids' ); $terms = get_terms($taxonomy_name, $term_args); if($terms){ $term_key = 'term_size'; $term_value = 'empty'; $term_unique = true; //go through all terms and set the new term meta foreach($terms as $term_id){ $term = get_term($term_id, $taxonomy_name); $term_count = $term->count; //determine new meta value if($term_count > 10){ $term_value = 'big'; }else if($term_count >= 5 && $term_count < 10){ $term_value = 'medium'; }else if($term_count >= 1 && $term_count < 5){ $term_value = 'small'; } //save meta value add_term_meta($term_id, $term_key, $term_value, $term_unique); } } } add_action('init', 'add_featured_to_categories');
$ TERM_ID -
category.phpまたは他の子テーマテンプレートファイル内で、用語データが表示される機能を変更できます。
//given a term, collect its saved image to be displayed function display_term_meta_image($term_id, $term_taxonomy){ //get supplied term $term = get_term($term_id, $term_taxonomy); if($term){ $term_image_id = get_term_meta($term_id, 'term_image_id', true); if($term_image_id){ //get the medium image size for display $term_image = wp_get_attachment_image_src($term_image_id, 'medium', false); echo '<img . $term_image[0] . '" title="' . $term->name . ' image"/>'; } } }
これは、このような説明の下に写真を表示します:
投稿の場合と同じように、用語メタデータを削除できます。 delete_term_meta関数を使用する場合、必要に応じて3番目のオプションで2つの必須パラメーターを提供する必要があります。
この関数は、$ Aldocation_term_keys変数で指定されていない追加のメタデータを通過して削除し、データベース内の無駄なスペースを削減します(不要になった数十メタデータエントリがある場合に役立ちます)。 > WordPress 4.3以降との逆方向の互換性
//saving new fields for category function save_extra_taxonomy_fields($term_id){ $term = get_term($term_id); $term_slug = $term->slug; //collect category image id from posted values $term_category_image_id = isset($_POST['category_image_id']) ? sanitize_text_field($_POST['category_image_id']) : ''; //update value and save it as an option update_option('category_image_id_' . $term_slug, $term_category_image_id); } add_action('create_category','save_extra_taxonomy_fields');
これらの新しいメタ関数を前進させることに本当に熱心であるが、古いバージョンで自分自身をカバーしたい場合は、すべての機能を確保するための条件付き機能を作成できます。
ご覧のとおり、これは少し長くなりますが、古いWordPressバージョンと新しいWordPressバージョンをサポートするための柔軟性が追加されます。
それをすべて包みますfunction add_featured_to_categories(){ //get all terms from the category taxonomy $taxonomy_name = 'category'; $term_args = array( 'orderby' => 'name', 'hide_empty' => false, 'fields' => 'ids' ); $terms = get_terms($taxonomy_name, $term_args); if($terms){ $term_key = 'term_size'; $term_value = 'empty'; $term_unique = true; //go through all terms and set the new term meta foreach($terms as $term_id){ $term = get_term($term_id, $taxonomy_name); $term_count = $term->count; //determine new meta value if($term_count > 10){ $term_value = 'big'; }else if($term_count >= 5 && $term_count < 10){ $term_value = 'medium'; }else if($term_count >= 1 && $term_count < 5){ $term_value = 'small'; } //save meta value add_term_meta($term_id, $term_key, $term_value, $term_unique); } } } add_action('init', 'add_featured_to_categories');
これらの新しいメタ関数を使用すると、用語をより簡単に拡張して一意の機能を提供できるようになります。たとえば、用語の一番上にバナー画像を追加したり、メタデータを提供して、条件付きで条件付きで異なる方法で表示できる場合があります(表示されている用語に基づいて新しいテンプレートファイルをロードするなど)。 新しい用語メタ関数の柔軟性と容易さにより、今日の新しいプロジェクトでこれを実装し始めることができます!
WordPress Term Meta
はい、WordPressでカスタム分類法で用語メタを使用できます。メタ機能という用語は、カスタムのものを含む任意の分類法で動作します。WordPressで用語メタを管理するのに役立つプラグインはありますか? WordPressでTerm Metaを管理します。これらのプラグインは、用語メタを追加、更新、削除するためのユーザーフレンドリーなインターフェイスを提供するため、コーディングに慣れていない人が簡単になります。以上がWordPress Term MetaおよびWP_Termの紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。