ワードプレスのthe_termsフィルタの使用方法・解説

概要

the_terms フィルタは、WordPressで投稿やカスタム投稿タイプのターム(タクソノミーの項目)を表示する際にカスタマイズするために使用されるフィルタです。このフィルタを利用すると、タームの出力形式や表示内容を変更することができます。以下は、the_terms フィルタがよく使われる機能の例です。

  1. タームの表示スタイルをカスタマイズ
  2. タームのリンクに特定のクラスを追加
  3. タームの名前を翻訳または変更
  4. タームの表示時に条件付きロジックを適用
  5. タームのカウントを表示
  6. 重複するタームをフィルター
  7. タームの順序を変更
  8. タームの表示をユーザーの権限に基づいて変更

構文

the_terms フィルタは以下のように構成されます。

apply_filters( 'the_terms', $terms, $post_id, $taxonomy );

パラメータ

  • $terms(配列):表示されるタームの配列。
  • $post_id(整数):タームが関連付けられている投稿のID。
  • $taxonomy(文字列):タクソノミーのスラッグ。

戻り値

フィルタ適用後のタームのデータを返します。

関連する関数

the_terms

ワードプレスのバージョン

the_terms フィルタは、全てのバージョンのWordPressで使用可能です。

コアファイルのパス

the_terms フィルタは、次のファイルに含まれています:
wp-includes/taxonomy.php

この関数のアクションでの使用可能性

アクション 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプルコード 1: タームのリンクに特定のクラスを追加

このサンプルコードは、タームを閲覧する際にリンクに特定のCSSクラスを追加します。

add_filter( 'the_terms', 'add_custom_class_to_term_links', 10, 3 );

function add_custom_class_to_term_links( $terms, $post_id, $taxonomy ) {
    if ( is_array( $terms ) ) {
        foreach ( $terms as $key => $term ) {
            $terms[$key] = '<a href="' . get_term_link( $term ) . '" class="custom-class">' . $term->name . '</a>';
        }
    }
    return $terms;
}

引用元: https://developer.wordpress.org/reference/hooks/the_terms/

サンプルコード 2: タームの表示時に条件付きロジックを適用

このサンプルコードは、特定の条件に基づいてタームを表示するか否かを決定します。

add_filter( 'the_terms', 'conditionally_display_terms', 10, 3 );

function conditionally_display_terms( $terms, $post_id, $taxonomy ) {
    if ( ! user_can( get_current_user_id(), 'edit_posts' ) ) {
        return '';
    }
    return $terms;
}

引用元: https://developer.wordpress.org/reference/hooks/the_terms/

サンプルコード 3: タームの順序を変更

このサンプルコードでは、タームの表示順序を変更します。

add_filter( 'the_terms', 'sort_terms_alphabetically', 10, 3 );

function sort_terms_alphabetically( $terms, $post_id, $taxonomy ) {
    if ( is_array( $terms ) ) {
        usort( $terms, function( $a, $b ) {
            return strcmp( $a->name, $b->name );
        });
    }
    return $terms;
}

引用元: https://developer.wordpress.org/reference/hooks/the_terms/

サンプルコード 4: タームの表示にカウントを追加

このサンプルコードは、各タームにつき、その投稿数を表示します。

add_filter( 'the_terms', 'add_term_count', 10, 3 );

function add_term_count( $terms, $post_id, $taxonomy ) {
    if ( is_array( $terms ) ) {
        foreach ( $terms as $key => $term ) {
            $count = $term->count;
            $terms[$key] = $term->name . ' (' . $count . ')';
        }
    }
    return $terms;
}

引用元: https://developer.wordpress.org/reference/hooks/the_terms/

サンプルコード 5: 重複するタームをフィルター

このサンプルコードは、重複するタームを表示しないようにします。

add_filter( 'the_terms', 'remove_duplicate_terms', 10, 3 );

function remove_duplicate_terms( $terms, $post_id, $taxonomy ) {
    if ( is_array( $terms ) ) {
        $terms = array_unique( $terms, SORT_REGULAR );
    }
    return $terms;
}

引用元: https://developer.wordpress.org/reference/hooks/the_terms/

この関数について質問する


上の計算式の答えを入力してください