WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding separators to categories (existing code) (3 posts)

  1. ggg377
    Member
    Posted 1 year ago #

    I have this php code to display categories:

    <span class="cat-links">
    <?php
    foreach((get_the_category()) as $category) {
        echo '<span id="'.$category->slug . '"><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a></span>'; $separator = ' ';
    }
    
    ?>
    </span>

    I looked up the codex for get_the_category and it says $separator = ' '; would add a space between the categories. But wherever I place it even when using the ; symbols properly, it either does nothing or it loses the odd-colored tag with a different class in my example (test). I would appreciate all help or hints.

    example: http://goldenred.web44.net/

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/get_the_category

    try:

    <span class="cat-links">
    <?php
    $separator = '';
    foreach((get_the_category()) as $category) {
        echo $separator . '<span id="' . $category->slug . '"><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a></span>'; $separator = ' ';
    }
    ?>
    </span>
  3. ggg377
    Member
    Posted 1 year ago #

    That did the trick alchymyth. Thanks a lot.

Topic Closed

This topic has been closed to new replies.

About this Topic