サポート » 使い方全般 » カスタム投稿のカテゴリ一覧で特定のカテゴリ名を除外して表示したい

  • 解決済 hellomac

    (@hellomac)


    function.php を編集してカスタム投稿タイプを wordpress に追加した際、カスタム分類(カテゴリ)も追加しました。
    カスタム投稿タイプのループやシングルページを表示する際、投稿の属するカテゴリ名を表示させたいのですが、一覧から除外したいカテゴリがありますがその方法がわかりません。

12件の返信を表示中 - 1 - 12件目 (全12件中)
  • hellomacさん

    カスタム投稿タイプのループやシングルページを表示する際、投稿の属するカテゴリ名を表示させたいのですが

    こちらは、single.php(カスタムポスト)内にカスタム分類を表示させ、特定のタームを除外したい感じですようね?
    であれば、下記の感じでいかがでしょうか?

    <?php
    // タクソノミーにはデフォルト値がないので下記を例として使う。
    $taxonomies = array(
        'custom-taxonomy',//カスタムカテゴリーのslug
    );
    $args = array(
        'exclude'       => array(559), //除外タームID
    );
    $terms = get_terms( $taxonomies, $args );
    $post_type = 'custom-post';//表示させたいカスタム投稿タイプ
    foreach($terms as $mycat):
    ?>
    
    (表示させたい内容)
    
    <?php
    endforeach;
    wp_reset_postdata();
    ?>

    hellomacさん
    検証はしてませんが、以前作ったソースから抜粋しました。
    カテゴリーリスト的な表示であれば、下記のような感じかもしれません。
    動かなかったらすみません。
    ※ほかの用途で作ったものの流用でしたので。

    <?php
    // タクソノミーにはデフォルト値がないので下記を例として使う。
    $taxonomies = array(
        'custom-taxonomy',//カスタムカテゴリーのslug
    );
    $args = array(
        'exclude'       => array(559), //除外タームID
    );
    $terms = get_terms( $taxonomies, $args );
    $post_type = 'custom-post';//表示させたいカスタム投稿タイプ
    echo '<ul>';
    foreach($terms as $mycat):
    //各項目の取得定義
    //$terms = get_the_terms();
    $term_id = $mycat -> term_id;
    $term_slug = $mycat -> slug;
    $cat_title = esc_html($mycat -> name);//カテゴリー名
    $cat_link = esc_url(get_term_link($mycat -> slug, $mycat -> taxonomy));
    ?>
    
      <li id="<?php esc_html($term_id);//いらなければ消して ?>">
        <a href="<?php echo $cat_link; ?>"><?php echo $cat_title; ?></a>
      </li>
    
    <?php
    endforeach;
    echo '</ul>';
    wp_reset_postdata();
    ?>
    トピック投稿者 hellomac

    (@hellomac)

    mura0403さん、ありがとうございます。

    上記の例確認しましたところ、カスタム投稿の全てのタームのリストから除外したタームのみ非表示になりました。
    やりたかったことはターム全てのリスト取得ではなくて、投稿につけたタームの中からあるタームだけ除外したかったのです。ネットで色々検索してみたのですが該当する項目が見つからず・・

    hellomacさん

    なるほどですね。
    ちょっと自信ないんですけど↓これだとどうですか??

    <?php
    // タクソノミーにはデフォルト値がないので下記を例として使う。
    $taxonomies = array(
        'custom-taxonomy',//カスタムカテゴリーのslug
    );
    $custom_terms = get_the_terms($post->ID, $taxonomies);
    $args = array(
    
    	//'include'       => array(),//指定IDのみ表示(空の場合は全部表示)
    	 'slug'          => array($custom_terms => slug), //表示させるslug
    	 'exclude'       => array(559), //除外タームID
    );
    $terms = get_terms( $taxonomies, $args );
    $post_type = 'custom-post';//表示させたいカスタム投稿タイプ
    echo '<ul>';
    foreach($terms as $mycat):
    //各項目の取得定義
    $term_id = $mycat -> term_id;
    $term_slug = $mycat -> slug;
    $cat_title = esc_html($mycat -> name);//カテゴリー名
    $cat_link = esc_url(get_term_link($mycat -> slug, $mycat -> taxonomy));
    ?>
    
      <li id="<?php esc_html($term_id);//いらなければ消して ?>">
        <a href="<?php echo $cat_link; ?>"><?php echo $cat_title; ?></a>
      </li>
    
    <?php
    endforeach;
    echo '</ul>';
    wp_reset_postdata();
    ?>
    トピック投稿者 hellomac

    (@hellomac)

    mura0403さん、ありがとうございます。
    できませんでしたぁ。。

    hellomacさん
    組みなおしました、下記コードでいかがでしょうか。
    綺麗ではないのですが、除外タームIDをIF文にしました。
    わたくしの環境では動きました。

    <?php
    $taxonomy = 'タクソノミーslug';
    $custom_terms = get_the_terms( $post->ID, $taxonomy );
    
    foreach ( $custom_terms as $custom_term ) :
    $custom_term_id = $custom_term->term_id;
     $args = array(
        'orderby'       => 'name',
        'order'         => 'ASC',
        'hide_empty'    => true,
        //'exclude'       => 550, //非表示にしたいID
        'exclude_tree'  => array(),
        'include'       => array($custom_term_id),//表示にしたいID
        'number'        => '',
        'fields'        => 'all',
        'slug'          => '',
        'parent'        => '',
        'hierarchical'  => true,
        'child_of'      => 0,
        'childless'     => false,
        'get'           => '',
        'name__like'    => '',
        'description__like' => '',
        'pad_counts'    => false,
        'offset'        => '',
        'search'        => '',
        'cache_domain'  => 'core'
    );
    $terms = get_terms( $taxonomy, $args );
    foreach ( $terms as $term ) :
    
    $term_id = $custom_term -> term_id;
    $term_slug = $custom_term -> slug;
    $cat_title = esc_html($custom_term -> name);//カテゴリー名
    $cat_link = esc_url(get_term_link($custom_term -> slug, $custom_term -> taxonomy));
    
    $exclude_id = 550;//除外タームID
    if ( $custom_term_id != $exclude_id ) ://非表示にしたいタームIDで条件付与
    ?>
    
      <li id="<?php esc_html($custom_term_id);//いらなければ消して ?>">
        <a href="<?php echo $cat_link; ?>"><?php echo $cat_title; ?></a>
      </li>
    
    <?php
    endif;
    endforeach;//END $terms as $term
    endforeach;//END $custom_terms as $custom_term
    wp_reset_query();
    ?>
    トピック投稿者 hellomac

    (@hellomac)

    mura0403さん、上記の方法で除外することができました。

    報告:ループ内では使えないみたいでした。

    トピック投稿者 hellomac

    (@hellomac)

    こんな書き方ができればなぁと思っているのですが能力がなかったです・・・。

    <?php
    $custom_terms = get_the_terms($post->ID, $taxonomy);
    $exclude = array(除外ID,除外ID,,,,);

    foreach(
    (array)$custom_terms as $custom_term)
    if(!in_array($custom_term->cat_ID, $exclude)
    )

    echo ‘cat_ID).'”>’.$custom_term->cat_name.’ ‘;
    ?>

    こんな書き方

    $custom_terms = get_the_terms( $post->ID, $taxonomy );
    $exclude = array( 除外ID, 除外ID, , , , );
    
    if ( $custom_terms != false && !is_wp_error($custom_terms) ){
    	foreach( $custom_terms as $custom_term ){
    		if( !in_array( $custom_term->term_id, $exclude ) ){
    			echo $custom_term->name.' ';
    		}
    	}
    }

    こんな感じでどうでしょうか。

    別案です。if function_exists の部分を functions.php へ切り出すとスッキリします。

    while ( have_posts() ) : the_post();	// Start the Loop.
    
    	if ( ! function_exists( 'my_exclude_terms' ) ) {
    		function my_exclude_terms( $a ) {
    			if ( is_array( $a ) ) {
    				$exc_iro = array();
    				// タクソノミー 'iro' のターム 'white' を除外
    				$exc_iro[] = get_term_by( 'slug', 'white', 'iro' );
    				return array_udiff( $a, $exc_iro, 'my_compare_term_slug' );
    			} else
    				return $a;
    		}
    
    		function my_compare_term_slug( $a, $b ) {
    			return strcmp( $a->slug, $b->slug );
    		}
    	}
    
    	echo '<div>'; the_date();
    	echo '&nbsp;'; the_title();
    	echo '&nbsp; - iro: ';
    
    	// 0 はループ内で現在の投稿。タクソノミー 'iro'
    	$iroiro = get_the_terms( 0, 'iro' );
    
    	$iroiro = my_exclude_terms( $iroiro );
    	if ( empty( $iroiro ) ) echo 'なし';
    	else {
    		foreach ( $iroiro as $iro )
    			echo ' ' . $iro->slug;
    	}
    	echo "</div>\n";
    
    endwhile;	// End the loop.

    トピック投稿者 hellomac

    (@hellomac)

    x743 さん、ありがとうございました。
    思ったような書き方になりました。

    トピック投稿者 hellomac

    (@hellomac)

    gblsm さん、ありがとうございました。
    なるほどです

12件の返信を表示中 - 1 - 12件目 (全12件中)
  • トピック「カスタム投稿のカテゴリ一覧で特定のカテゴリ名を除外して表示したい」には新たに返信することはできません。