1

カスタム分類法を設定したカスタム投稿タイプがあります。

投稿が含まれているカテゴリ(カスタム分類)を印刷したいのですが、除外します。ただし、カテゴリを除外するための解決策が見つかりません。カスタム投稿タイプがファイルされているカテゴリのリストを出力するための私のコードは次のとおりです。

<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>

特定のカテゴリを除外するにはどうすればよいですか?

ありがとう。

4

1 に答える 1

2

functions.php ファイルにget_the_terms、用語のリストを配列として返す関数を作成し、不要な項目を削除することができます。

これを試してください:

function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
    $terms = get_the_terms( $id, $taxonomy );

    if ( is_wp_error( $terms ) )
        return $terms;

    if ( empty( $terms ) )
        return false;

    foreach ( $terms as $term ) {
        if(!in_array($term->term_id,$exclude)) {
            $link = get_term_link( $term, $taxonomy );

            if ( is_wp_error( $link ) )
                return $link;

            $excluded_terms[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
            }
    }

    $excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );

    return $before . join( $sep, $excluded_terms ) . $after;
}

そして、次のように使用します。

<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>
于 2012-04-11T18:13:01.143 に答える