Jump to content

Wordpress/Php help


danser81

Recommended Posts

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 by requinix
Link to comment
Share on other sites

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.    

Link to comment
Share on other sites

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>

 

genesis-circular-categories.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.