Support » Fixing WordPress » Problem with separator

  • Resolved greencode

    (@greencode)


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael

    (@alchymyth)

    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 = ' / ';
    }}
    ?>

    greencode

    (@greencode)

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

    Michael

    (@alchymyth)

    ok, i get it 😉

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

    $sep = '';

    greencode

    (@greencode)

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Problem with separator’ is closed to new replies.