WordPress.org

Ready to get started?Download WordPress

Forums

PHP - Separate returned values with space? (2 posts)

  1. rjpinney
    Member
    Posted 1 year ago #

    I am using this code to output the child category slugs that fall within a particular parent category.

    <?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(5, $childcat)) { echo $childcat->slug; }} ?>

    The problem is that when there is more than one category, they are all inserted as one word - I need to separate them with a space. My guess is that I need to use implode for this (?), but I'm not quite sure how to implement it.

    Any ideas?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    if you don't mind an extra space at the end, try:

    <?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(5, $childcat)) { echo $childcat->slug . ' '; }} ?>

Topic Closed

This topic has been closed to new replies.

About this Topic