How can I add space next to an echo of categories assigned to a post:
<?php
$category_detail=get_the_category($post->ID);
foreach($category_detail as $cd)
{
echo $cd->slug;
}
?>
The above code prints all categories assigned to a post, but it prints it like this:
category1category5
how can I add a space between each category printed? ie category1 category5
with some magic:
What is the logic behind the code is: we will test that if this is the first id to print or not. If this is the first id to print then we won’t do anything. But if it’s not the first id then we will print a space before printing it. That way every id will be separated by a single space and there will be no leading or trailing spaces.
Code:
Try this
I usually do something simple like this:
No trailing or leading space.
Here is another version using implode: