2013-04-01 2 views
0

해당 웹에서 모든 답변을 검색합니다. wp_list_categories를 사용하여 사용자 정의 분류법을 사용하여 하위 메뉴를 만들었습니다.이 카테고리는 잘 작동하고 해당 카테고리를 탐색 할 때 current-cat을 넣습니다.맞춤 분류가있는 싱글에 wp_list_categories에 current_cat 클래스를 추가하십시오.

이 메뉴를 사용하여 단일 게시물을 탐색하면 강조 표시가 더 이상 작동하지 않습니다. 해당 사이트의 블로그의 경우

, 나는 wp_list_categories()에 현재 카테고리 강조하기 위해 다음과 같은 코드를 사용

function sgr_show_current_cat_on_single($output) { 

global $post; 

if(is_single()) { 

$categories = wp_get_post_categories($post->ID); 

foreach($categories as $catid) { 
    $cat = get_category($catid); 
    if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) { 
    $output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output); 
    } 

} 

} 
return $output; 
} 

add_filter('wp_list_categories', 'sgr_show_current_cat_on_single'); 

그러나 지금까지의 내가 시도로, 그것은 하나의 게시물 작동 할 수 없습니다를 그 사용자 지정 분류 체계에 따라 정렬됩니다. : /> 나는 그것을 어떻게 사용자 정의 해야할지 모르겠다.

심지어 가능합니까?

답변

1

맞춤 분류학 용어 ID를 얻으려면 wp_get_post_categories(); 대신 get_the_terms($id, $taxonomy);을 사용해야합니다.

분류 체계 이름을 functon에 하드 코딩하거나 $args (wp_list_categories($args);)으로 전달할 수 있습니다.

최종 코드 :

add_filter('wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2); 

function sgr_show_current_cat_on_single($output, $args) { 

    if (is_single()) : 

    global $post; 

    $terms = get_the_terms($post->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); 
     } 

    } 

    endif; 

    return $output; 

} 
관련 문제