Jump to content

Add Separator to Wordpress Function

Go to solution Solved by cyberRobot,

Recommended Posts

Hi guys, i'm attempting to display Wordpress Categories per Post.


I'm aware of this simple standard method

<?php the_category( ', ' ); ?>.

But i actually just want a list of the categories, unformatted and unlinked to the category itself, actually i just want the category name.


So i am using this method

<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>

This works perfectly if the post only has 1 category, but in the event of multiple categories, there is no separator. Could anybody assist me in adding a separator which will not append to the last in the list ?


I would be grateful for any assistance but please bare in mind i am a mere novice.



Link to comment
Share on other sites

Thanks for pointing me in the correct direction .. So i worked out a solution, but baring in mind i am an absolute newb, would you mind telling me if this is the cleanest way to achieve what i want.


It works perfectly !


$categories = get_the_category();
$category_names = array();
      foreach ($categories as $category)
           $category_names[] = $category->cat_name;
           echo implode (',', $category_names); 
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.

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.