[ 解決済 ] カスタム投稿のカテゴリーの色を参照したい。

VWSとは フォーラム Lightning [ 解決済 ] カスタム投稿のカテゴリーの色を参照したい。

[ 解決済 ] カスタム投稿のカテゴリーの色を参照したい。

  • このトピックには3件の返信、2人の参加者があり、最後にgundamにより14時間、 43分前に更新されました。
4件の投稿を表示中 - 1 - 4件目 (全4件中)
  • 投稿者
    投稿
  • #95977

    gundam
    参加者

    ■ WordPress のバージョン
    6.5.5

    ■ テーマ・プラグインは全て最新版で確認してください。
    確認しました。

    ■ テーマの種類
    Lightning ( G3 ) + Lightning G3 Pro Unit

    ■ テーマのバージョン
    15.25.1

    ■ Pro Unit のバージョン
    0.26.8

    ■ スキンの種類
    Origin III

    ■ 期待する動作
    カスタム投稿ページのカテゴリーを設定した際のカラーを表示させたいです。

    ■ 自分で試した事

    add_action(
        'lightning_entry_body_before',
        function () {
            global $post;
            if (is_single() && 'information' === get_post_type()) {
                $info_cat = get_the_term_list($post->ID, 'info-cat');
    
                $info_cat_color = '';
                $info_cat_terms = get_the_terms($post->ID, 'info-cat');
                if ($info_cat_terms && !is_wp_error($info_cat_terms)) {
                    foreach ($info_cat_terms as $term) {
                        $color = get_term_meta($term->term_id, 'postList_terms', true);
                        if ($color) {
                            $info_cat_color = $color;
                            break; // 最初に見つかった色を使用する
                        }
                    }
                }
    
                $info_time = get_the_date();
                echo '<header class="p-entry_header">';
                echo '<div class="p-entry_meta">';
                echo '<div class="p-entry_cat">' . $info_cat . $info_cat_color . '</div>';
                echo '<div class="p-entry_date">' . $info_time . '</div>';
                echo '</div>';
                echo '<h1 class="p-entry_title">' . get_the_title() . '</h1>';
                echo '</header>';
            }
        }
    );

    このコードでは、カテゴリー名に続いて色番が文字列で表示されるようになっています。まずは色の取得をしています。
    カスタムフィールド名が分からず、ダメなのか、コードが悪いのか、実際上記のコードでは「空文字」です。カテゴリ名しか表示されません。

    color = get_term_meta($term->term_id, ‘postList_terms’, true);
    postList_termsここがカスタムフィールド名なのですが、わからなかったです。

    よろしくお願いいたします。

    ■ 症状が発生するブラウザ
    safari

    ■ 実際の症状
    提示したコードでは、色を取得する事ができなかったです。

    #96149

    gundam
    参加者
    add_action(
    	'lightning_entry_body_before',
    	function () {
    		global $post;
    		if ( is_single() && 'information' === get_post_type() ) {
    			$info_terms       = get_the_terms( $post->ID, 'info-cat' );
    			$info_time        = get_the_date();
    			$categories_class = '';
    
    			if ( ! empty( $info_terms ) && ! is_wp_error( $info_terms ) ) {
    				foreach ( $info_terms as $term ) {
    					$categories_class .= ' category-' . esc_attr( $term->slug );
    				}
    			}
    
    			echo '<div class="custom-info-cat' . $categories_class . '">';
    			echo '<span class="custom-info-cat-date">' . $info_time . '</span>';
    			echo '<span class="custom-info-cat-category">' . get_the_term_list( $post->ID, 'info-cat', ", ', ', " ) . '</span>';
    			echo '</div>';
    			echo '<h1 class="entry_title">' . get_the_title() . '</h1>';
    		}
    	}
    );

    とりあえず、カスタム投稿のカテゴリースラッグを参照して、cssで背景色をつける事にしましたが
    カテゴリーの色を変えると、当然、別色になってしまいます。

    何かいい方法は、ないでしょうか?
    よろしくお願いいたします。

    #96216

    以下でわかりますか?

    use VektorInc\VK_Term_Color\VkTermColor;
    
    add_action(
    	'lightning_entry_body_before',
    	function () {
    
    		global $post;
    		$args = array(
    			'taxonomy' => 'area', // 対象のタクソノミーを指定
    		);
    		if ( class_exists( VkTermColor::class ) && method_exists( VkTermColor::class, 'get_post_single_term_info' ) ) {
    			$term = VkTermColor::get_post_single_term_info( $post, $args );
    			echo '<pre style="text-align:left">';
    			print_r( $term );
    			echo '</pre>';
    		}
    	}
    );
    #96232

    gundam
    参加者

    石川様 ありがとうございます。下記の通り色を参照する事ができるようになり、
    基本のカテゴリー色を変えても、その色にする事ができました。
    ありがとうございます。

    use VektorInc\VK_Term_Color\VkTermColor;

    add_action(
    ‘lightning_entry_body_before’,
    function () {
    global $post;

    if (is_single() && ‘information’ === get_post_type()) {
    $info_terms = get_the_terms($post->ID, ‘info-cat’);
    $info_time = get_the_date();

    $args = array(
    ‘taxonomy’ => ‘info-cat’, // 対象のタクソノミーを指定
    );

    $background_color = ”;
    if (class_exists(VkTermColor::class) && method_exists(VkTermColor::class, ‘get_post_single_term_info’)) {
    $term = VkTermColor::get_post_single_term_info($post, $args);
    if (isset($term[‘color’])) {
    $background_color = $term[‘color’];
    }
    }

    echo ‘<div class=”custom-info-cat”>’;
    echo ‘<span class=”custom-info-cat-date”>’ . $info_time . ‘</span>’;
    echo ‘<span class=”custom-info-cat-category” style=”background-color:’ . esc_attr($background_color) . ‘;”>’ . get_the_term_list($post->ID, ‘info-cat’, ”, ‘, ‘, ”) . ‘</span>’;
    echo ‘</div>’;
    echo ‘<h1 class=”entry_title”>’ . get_the_title() . ‘</h1>’;
    }
    }
    );

4件の投稿を表示中 - 1 - 4件目 (全4件中)
  • このトピックに返信するにはログインが必要です。