Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:06:51+00:00 2026-05-16T16:06:51+00:00

I currently have a site with a hard-coded list of categories & subcategories, where

  • 0

I currently have a site with a hard-coded list of categories & subcategories, where each item is formatted as follows:

<li class="cat-item open-access"><a href="/categories/open-access/">Open Access</a></li>

Importantly, each <li> item is assigned a class which matches the slug of the category linked to within.

I would obviously like to use WordPress’ wp_list_categories() to output the list instead of hard-coding it, but need to keep the custom class for each <li> item.

I have been looking into filters and actions and thought I might have come up with a fix by adding this to my theme’s functions.php file:

function add_class_from_slug($wp_list_categories) {
        $pattern = '/class=\"/';
        $replacement = 'class="'.$category->slug.' ';
        $newclass = preg_replace($pattern, $replacement, $wp_list_categories);
        return $newclass;
}
add_filter('wp_list_categories','add_class_from_slug');

But this doesn’t work — the text returned by $category->slug drops out when the page is rendered. If I add in static text (using a line like $replacement = 'class="myclass ';, it renders fine.

Frustratingly, I can get the output I want by adding $class .= ' '.$category->slug;
at the right spot wp-includes/classes.php, but want to avoid resorting to that.

Why can’t I just use $category->slug in my function? Workarounds, suggestions, further reading on the subject? Should add that I have a pretty basic grip on PHP. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:06:52+00:00Added an answer on May 16, 2026 at 4:06 pm

    You can extend the Walker_Category class with your own class and override the function start_el().

    If the name of the class you used is Walker_MyCategory, you could call wp_list_categories() like:

    wp_list_categories('walker=Walker_MyCategory')
    

    You could also build your own list yourself. See get_categories()


    Oh and your code didn’t work because $category wasn’t defined anywhere. You need to know exactly what category it is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a site which uses a MySQL 4.x schema, the site uses
Currently I have a site that is set up using a masterpage and a
Currently I have a site where it has a header that looks something like
I currently have a web site hitting 7 Access DBs that I desperately need
I currently have a web site where users can select a custom theme. After
I currently have a django site, and it's kind of slow, so I want
In Magento, I currently have on the site category urls which have no trailing
We currently have a single installation multi-site setup, hosted in Europe, and are looking
Currently we have a project to implement an Internet Banking site, and we are
Currently I have: RewriteCond %{HTTP_HOST} ^site\.com [NC] RewriteRule (.*) http://www.site.com/$1 [R=301,L] When I visit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.