I'm building a jQuery masonry type gallery in Wordpress, which works off categories. I'm 90% there but need to display the categories in a data-groups tag for it to work. ie:

<div class="project" data-groups='["category1","category2",category3"]'>

I'm using to following function:

function the_category_unlinked($separator = ' ') {
    $categories = (array) get_the_category();

    $thelist = '';
    foreach($categories as $category) {
       $thelist .= "&quot;" . $category->category_nicename . "&quot;,";

    echo $thelist;


<div class="project" data-groups='<?php the_category_unlinked(' '); ?>'>

which works, but this echos out the categories with a comma at the end and breaks the entire working effect:

<div class="project" data-groups='["category1","category2",category3",]'>

What can I do to display these categories as needed? It could also be done using tags if that would work...

1 Answer 1


Just remove the trailing comma with some good ole php before echoing it out.

$thelist = rtrim($thelist, ',');
  • Well well - I always seem to be just 1 step away from having it working! Thanks @aargh-a-Knot!
    – Dave
    Commented Nov 25, 2014 at 9:32
  • No problem, glad I could help.
    – SkyShab
    Commented Nov 25, 2014 at 13:19

Your Answer

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

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