WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem with separator (5 posts)

  1. greencode
    Member
    Posted 2 years ago #

    I'm using the following in my query to display the child categories

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(1, $childcat)) {
    echo $sep,'<a href="'.get_category_link($childcat->cat_ID).'">',$childcat->cat_name,'</a>';
    $sep = ' / ';
    }}
    ?>

    So this is then displaying as follows:

    Category a / Category b / Category c

    The problem is when there's more than one post on the page the next lot show as

    / Category a / Category b / Category c

    i.e. it's just continuing to add the forward slash and not simply starting the query again.

    Basically I need it so the separator does not show at the beginning or end of the category list.

    Any help would be greatly appreciated.

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    there is a comma after $sep which might break the code - should be a dot;

    try:

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(1, $childcat)) {
    echo $sep.'<a href="'.get_category_link($childcat->cat_ID).'">',$childcat->cat_name,'</a>';
    $sep = ' / ';
    }}
    ?>
  3. greencode
    Member
    Posted 2 years ago #

    Thanks for that - I've changed the comma to a full stop but it hasn't had any effect to the original issue.

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    ok, i get it ;-)

    before the foreach, you need to reset the separator to an epmty space:

    $sep = '';

  5. greencode
    Member
    Posted 2 years ago #

    Perfect - thanks so so much for your help. Really appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.