CG
(@du-bist-der-lenz)
カスタム投稿タイプの記事を、サイドバーで、カテゴリー毎にリスト表示したいということでしょうか。
CGさん、ご回答ありがとうございます。
カスタム投稿タイプのカテゴリーのリストをサイドバーに一覧に表示し、
その記事が属するもののリンクにクラスを追加し色を変えたいです。
例えば、
記事(single-xxx.php)が
カテゴリー1に属する場合
<ul>
<li><a href="#" class="active">カテゴリー1</a></li>
<li><a href="#">カテゴリー1</a></li>
<li><a href="#">カテゴリー1</a></li>
</ul>
上記のようにクラスを追加できたらと思います
CG
(@du-bist-der-lenz)
カスタム投稿タイプのページに限り、サイドバーでカテゴリー別一覧を表示し、色分けしました。
(右サイドバー「特集記事」下)
CG
(@du-bist-der-lenz)
それは「カテゴリーの取得を教えて下さい」が、簡潔な問いになりませんか。
<ul>
<li><span class=?php the_category(', ') ?></span><a href="#">カテゴリー1の投稿</a></li>
<li><span class=?php the_category(', ') ?></span><a href="#">カテゴリー2の投稿</a></li>
<li><span class=?php the_category(', ') ?></span><a href="#">カテゴリー3の投稿</a></li>
</ul>
現状、どのような方法(カテゴリーウィジェット、コード)で試されていますか?
CGさま、上記画像だと
例えば、「1962」に属する記事(single-xx.php)を閲覧時に「1962」だけ色を変えたいので、「1962」要素にクラスを追加したいです。
ishitakaさま、
single-xx.phpのサイドバーに
<ul>
<?php wp_list_categories(
array('title_li' => '', 'taxonomy' => 'タクソノミー名')
); ?>
</ul>
で一覧を表示させています。
wp_list_categories() の current_category パラメーターをタクソノミーに対応させるのはどうでしょうか?
<ul>
<?php wp_list_categories(
array('title_li' => '', 'taxonomy' => 'タクソノミー名', 'current_category' => true )
); ?>
</ul>
※ current_category を追加
functions.php に、
function my_list_categories( $output, $args ) {
if ( $args['taxonomy'] == 'タクソノミー名' && $args['current_category'] ) {
$terms = get_the_terms( get_the_ID(), $args['taxonomy'] );
foreach( $terms as $term ) {
if ( preg_match( "/cat-item-{$term->term_id}/", $output ) ) {
$output = str_replace( "cat-item-{$term->term_id}", "cat-item-{$term->term_id} current-cat", $output );
}
}
return $output;
}
}
add_filter( 'wp_list_categories', 'my_list_categories', 10, 2 );
CG
(@du-bist-der-lenz)
こうでしょうか、そして、なおかつ、「1962」をカテゴリに持つ、ページにとどまっている時に限るとの追加要望ですか。
ishitaka さま、
ありがとうございます。早速実装してみます。
CGさま、
はい、貼付画像のようにしたいです。