[resolved] How to replace the comma in the_category output (4 posts)

  1. malcolmcoles
    Posted 7 years ago #

    I'm using the_category to show the parent category and child category of individual posts (which are assigned to one child category only).

    I'm doing so on the post page (I know it says to do so within the loop here, but it seems to work outside the loop).

    Anyway, this returns something like: Parent,Child with both parent and child already the correct links. This is 95% of what I need.

    However, I don't like the way there is no space before or after the comma - and ideally, I want to replace the comma with something to indicate the parent/child relationship, like a '>'.

    So I've tried this:

    str_replace(',',' > ', the_category(' ', 'multiple'));

    But it doesn't work.

    I've also tried doing something similar having done
    $variable = the_category(' , ', 'multiple'))
    and then doing the str_replace on %variable, but that didn't work either.

    Any ideas ...? Thanks.

  2. malcolmcoles
    Posted 7 years ago #

  3. Shane G
    Posted 7 years ago #


    You can do it with the simple code modification..for that please refer below mentioned URL:


    Shane G.

  4. malcolmcoles
    Posted 7 years ago #

    Hi, Shane. I'm trying to use the_category to generate a breadcrumb trail. By default, the_category will only output categories that a post is on - it won't return the parent category (unless you put the post in that as well, but I don't want to).

    You CAN get the_category to return the parent category too by using the 'multiple' setting. But then you get it like this:
    - Parent,Child
    with no space between them - hence my attempt to replace the ',' with ' , ', or preferably ' > '.

    Anyway, the change outlined in my link above allows you to change the separator for the Parent,Child values displayed. This means you can then use the_category to generate a breadcrumb trail using just one line of PHP, rather than the usual overly-complicated ways or plugins.

    I go into some detail in my post on plugin-free breadcrumb trails if you're interested.

    Although let me know if I'm missing something obvious ...

Topic Closed

This topic has been closed to new replies.

About this Topic