0

I´m trying to highlight (using css), the child and parent categories in use in the selected post.

So I´ve found this plugin "Kahi's Highlight Used Categories".

Plugin´s website: http://kahi.cz/wordpress/highlight-used-categories-plugin/

What it does...

It adds a 'used-cat' class and 'used-cat-parent' class to stylize, to the sub-category and main category respectively.

The problem is:

My website (http://www.mpn.p.ht/wp_br2/), has three main categories "Masculino", (means male), "Feminino", (means female), and "Infantil", (means infant).

The sub-categories are the same for all the three main categories.

So, let´s say, we are at the post page with main category "Masculino", and sub-category "Acessórios", (means acessories)...

The plugin, seems to add 'used-cat' to all "Acessórios" (sub-category), found also in "Feminino" , and in "Infantil".

I would like the plugin to add the class 'used-cat', only to the sub-category of the selected post. Not to all sub-categories found with the same name, through the categories list.

Here´s the plugin code:

<?php
/*
Plugin Name: Kahi's Highligh Used Categories
Plugin URI: http://www.kahi.cz/wordpress/highlight-used-categories-plugin
Description: In the list of categories, adds classes <code>used-cat</code> and 
<code>used-cat-parents</code> to particular list-items (when browsing a post).

Version: 1.0
Author: Peter Kahoun and Dirk Jaster
Author URI: http://www.kahi.cz/wordpress/
*/

class KHUC {

function wp_list_categories ($text) {

    global $post;

    if (is_singular()) {

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

        foreach ($categories as $category_id) {

            $category = get_category($category_id);
            $category_parent = get_category($category->category_parent);

            $text = preg_replace(
                "/class=\"(.*)\"><a ([^<>]*)>$category->name<\/a>/", 
                ' class="$1 used-cat"><a $2>' . $category->name .    
'</a>', 
                $text);

            $text = preg_replace(
                "/class=\"(.*)\"><a ([^<>]*)>$category_parent-
>name<\/a>/", 
                ' class="$1 used-cat-parent"><a $2>' . 
$category_parent->name . '</a>', 
                $text);

        }

    }

    return $text;

}

}

add_filter('wp_list_categories', array('KHUC','wp_list_categories'));

Does anyone know, what changes in the plugin code should I make, so I get only the sub-category in use to get the 'used-cat' class?

3
  • What´s the matter with the question? Apr 18, 2013 at 23:43
  • Once you've hacked this plugin, how are you going to deal with updates to it?
    – vancoder
    Apr 18, 2013 at 23:56
  • 2
    "I quit working with WordPress, comments are closed. My plugins will not be updated any more – at least not by me. Feel free to modify my source codes though… Also I am not able to provide support, sorry. –Kahi" The Plugins Author. You can check it here at the bottom of the page, comments part: kahi.cz/wordpress/highlight-used-categories-plugin Apr 18, 2013 at 23:59

1 Answer 1

0

Use the category ID:

class KHUC {
    function wp_list_categories ($text) {
        global $post;
        if (is_singular()) {
            $categories = wp_get_post_categories($post->ID);
            foreach ($categories as $category_id) {
                $category = get_category($category_id);
                $category_parent = get_category($category->category_parent);
                $text = preg_replace(
                    '#class="([^"]*cat-item-'.$category->cat_ID.'[^"0-9]*)"#',
                    'class="$1 used-cat"', 
                    $text);
                $text = preg_replace(
                    '#class="([^"]*cat-item-'.$category_parent->cat_ID.'[^"0-9]*)"#',
                    'class="$1 used-cat-parent"', 
                    $text);
            }
        }
        return $text;
    }
}
add_filter('wp_list_categories', array('KHUC', 'wp_list_categories'));
7
  • The code didn´t generated the 'used-cat' and 'used-cat-parent' in the li elements of the category list. Link: mpn.p.ht/wp_br2/?p=37 Apr 19, 2013 at 0:18
  • classes 'used-cat' and 'used-cat-parent', not added Apr 19, 2013 at 0:27
  • I saw you have modified the code..thanks. But, still classes not added. I think WordPress has a 'wp_list_categories' function with the argument 'current_category' that forces the use of the class 'current-cat', even in a post page. But I´m not sure if it adds also 'current-cat-parent'. Those are the classes I used to highlight the categories in use in the categories page. Apr 19, 2013 at 0:36
  • Doesn´t, misses the classes. Apr 19, 2013 at 0:43
  • <div class="grid_4 omega"> <div id="sidebar"> <div id="largewidget" class="widgetlist alpha grid_4"> <?php if (function_exists('woo_sidebar') && woo_sidebar(1) ) : else : ?> <?php endif; ?> </div> </div><!-- /sidebar --> </div><!-- /grid 6 --> That´s the code of 'sidebar.php' file. Which is what I believe generates the list. Apr 19, 2013 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.