Comment obtenir current_cat dans la barre de navigation, en un seul post
1 réponses
- votes
J'aitrouvé la réponse ici .
Ajoutez àfunctions.php lafonctionet le crochet suivants:
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);
// Find cat-item-ID in the string
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');
I found the answer here.
Add to functions.php the following function and hook:
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);
// Find cat-item-ID in the string
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');
Dans un site quej'utilise Wordpress 3.0,lorsqueje suis dans un seul article,labarre denavigation affichant les catégoriesne donnepas à la catégorieparente la classe "current_cat"et donc cette catégorien'estpasmiseen évidence.
Commentpuis-jefaireen sorte que Wordpress attribue cette classe à la catégorieparenteen mode single_post?