ある分類のすべての分類用語をリストするために私自身の関数を書きました…
function wr_list_taxonomy($taxonomy, $orderby, $hierarchical) {
$show_count = 0;
$pad_counts = 0;
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
return wp_list_categories( $args );
}
そのため、この関数はwp_list_categories()
とまったく同じように機能し、term-pageにアクセスすると「current-cat」クラスも表示します。
URLの構造を想像してみてください。
www.mysite.com/term //current-cat works perfect
www.mysite.com/term/a-post-associated-with-this-term //current-cat not assigned
上に指摘したのと同じように、私が投稿になったらこの "カテゴリ"内で "current-cat"クラスを機能させる機会もありますか?
更新:
wr_list_taxonomy()
関数は私のheader.php
ファイルの中で呼ばれています。
<nav id="main-nav">
<ul class="wr-nav" role="navigation">
<?php
global $post;
$taxonomy = 'event_type';
$term_id = 0;
$terms = wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "ids") );
if ( !empty($terms) )
$term_id = $terms[0];
wr_list_taxonomy($taxonomy, 'name', 1, $term_id); ?>
</ul>
</nav>
wr_list_taxonomy()
関数をあなたのバージョンに更新しました。
さらに私が欲しいものに関連するかもしれない別の機能があります…
/**
* Add category-slug as classname to wp_list_categories()
*/
add_filter('wp_list_categories', 'add_slug_css_list_categories', 10, 2);
function add_slug_css_list_categories($list, $args) {
if ( $args["taxonomy"] == "event_type" ) {
$cats = get_terms('event_type');
$class = 'term term-';
} else {
$cats = get_categories();
$class = 'category-';
}
foreach( $cats as $cat ) {
$find = 'cat-item-' . $cat->term_id . '"';
$replace = $class . $cat->slug . '"';
$list = str_replace( $find, $replace, $list );
$find = 'cat-item-' . $cat->term_id . ' ';
$replace = $class . $cat->slug . ' ';
$list = str_replace( $find, $replace, $list );
}
return $list;
}
この関数はwr_list_taxonomy()
関数の各<li>
項目に "category-slug"を追加します。これはうまくいきます。
私が所属している "現在のカテゴリ"に関連付けられているsingle.php(投稿)サイトにいる場合は、 "current-cat"クラスも適用する必要があります。
はい、もちろんです。あなたはただidという用語を取得してそれを引数に置くだけです。チェック wp_list_categories()
function wr_list_taxonomy($taxonomy, $orderby, $hierarchical, $cat_id) {
$show_count = 0;
$pad_counts = 0;
$title = '';
$cat_id = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'current_category' => $cat_id
);
return wp_list_categories( $args );
}
カテゴリIDまたは用語IDを渡す必要があります。それを機能させるために。
カテゴリIDはどのように取得しますか?
例えば:
global $post;
$taxonomy = 'my-tax';
$term_id = 0;
if(is_singular('post')){ // post type is optional.
$terms = wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "ids") );
if(!empty($terms))
$term_id = $terms[0]; //we need only one term id
}
wr_list_taxonomy($taxonomy, $orderby, $hierarchical, $term_id);
これは単なる単純な例ですが、他にも方法があります。コードを使用している場所によって異なります。