danser81 Posted June 26, 2018 Share Posted June 26, 2018 (edited) I am trying to replace the rel="category tag" on my wordpress categories list with a custom class for each category id based on the category slug but I'm not sure what I'm doing wrong (am very elementary status with php). Or I could use Advanced Custom Fields and insert the custom field for the custom class, but still not sure how to insert the php as the class? Any guidance where I am going wrong? As always, thanks for any help!! function snip_category_rel($result) { $term = get_the_category($id); $cls = $term->slug; $result = str_replace('rel="category tag"', 'class="'. $cls .'"', $result); return $result; } add_filter('the_category', 'snip_category_rel'); add_filter('wp_list_categories', 'snip_category_rel'); Edited June 26, 2018 by requinix Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 26, 2018 Share Posted June 26, 2018 This is truly hacking your site in the worst possible interpretation of the word. The rel attribute has nothing at all to do with a class of the category tag. Wordpress already outputs a custom class in the category list. The code that does this is in Walker_Category and inspection of the page in chrome tools should reveal the inclusion of those css classes in the form of 'cat-item-' . $category->term_id So all that should be required is actually adding the appropriate css style for each individual category. Your posts seem to have the recurring issue that you start with: "I want to do <insert specific thing>" rather than, "My client wants <description> functionality on their Wordpress x.x site." The quality of the help you get will be much better if you start with the Macro view of the problem, then drill into the specifics. Quote Link to comment Share on other sites More sharing options...
danser81 Posted June 26, 2018 Author Share Posted June 26, 2018 Then I will rephrase my question...My client needs the site to be color coded by category (similar to the photo attached) so I need to be able to add a custom class to each individual category (preferably by slug name for id purposes) to be able to style them as such, including in the category list on each post. The code is not currently outputting a custom class for each category in the category list and this was the solution that I found after a lot of searching. I would have thought color coding categories by category would be a common occurrence but it doesn't appear so. I'm working locally but this is the output of the categories code currently and it appears Genesis framework is using get_the_category_list to retrieve the category list: <footer class="entry-footer"> <p class="entry-meta"> <span class="entry-categories term-color">Filed Under: <a href="http://home-reading-helper.local/grade-level/kindergarten/" rel="category tag">Kindergarten</a>, <a href="http://home-reading-helper.local/grade-level/kindergarten/reading/" rel="category tag">Reading</a></span> <span class="entry-tags">Tagged With: <a href="http://home-reading-helper.local/activity/home-activity/" rel="tag">Home Activity</a></span> </p> </footer> Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted June 27, 2018 Share Posted June 27, 2018 Have you viewed the source to see if there's a selector you can use in CSS so you don't have to mess with the php at all. https://www.w3schools.com/cssref/css_selectors.asp Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.