Forums » Fixing WordPress » How to remove comma from end of category list

  • Resolved danpalehorse

    (@danpalehorse)


    Hi there, I’m using the following code to recall a list of categories whilst removing the link.

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

    But unfortunately it is adding a comma to the last cat as well.

    I’ve had a look around and can’t find a working fix, can anyone help?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You could do something like this:

    <?php
    		$categories = get_the_category();
    		$total = count ($categories);
    		$i=1;
    		foreach($categories as $category) {
    		if ($i < $total) {
    			echo $category->cat_name . ', ';
    		} else {
    			echo $category->cat_name;
    		}
    		$i++;
    		}
    ?>
    Thread Starter danpalehorse

    (@danpalehorse)

    Fantastic!

    Worked a treat, thanks a lot.

    D

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to remove comma from end of category list’ is closed to new replies.