Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using a function (copied and modified a bit from this question ) which displays taxonomies in order by rank with » between parent and child and | between terms, but I get a mistake in the html output and I don't understand what's wrong.

Here's what I put in my functions php (the function I copied from WP questions):

function print_taxonomy_ranks( $terms ) {
    // if terms is not array or its empty don't proceed
    if ( ! is_array( $terms ) || empty( $terms ) ) {
        return false;
    }

    foreach ( $terms as $term ) {
        // if the term have a parent, set the child term as attribute in parent term
        if ( $term->parent != 0 )  {
            $terms[$term->parent]->child = $term;   
        } else {
            // record the parent term
            $parent = $term;
        }

    echo "<a href='http://blogadress/$parent->taxonomy/$parent->slug'>$parent->name</a> » <a href='http://blogadress/{$parent->child->taxonomy}/{$parent->child->slug}'>{$parent->child->name}</a> | ";

  }
}

(Note: I'm using absolute path http://blogadress because I'm running it locally and I'm not managing to make it work otherwise)

And here's what I put in my template file

<p><?php print_taxonomy_ranks( get_the_terms( $post->ID, 'soggetto' ) ); ?></p>

Here's the WRONG html output I get:

error1: the first two terms don't appear (just a link to the blog home with a double slash at the end, linked to nothing);

error 2 (unwanted behavior): the terms parents appear two times: once as solitary terms, and once as parents of the children.

<p><a href='http://blogadress//'></a> » <a href='http://blogadress//'></a> | 
<a href='http://blogadress/soggetto/parent1'>parent1</a> » <a href='http://blogadress/soggetto/child1'>child1</a> | 
<a href='http://blogadress/soggetto/parent2'>parent2</a> » <a href='http://blogadress//'></a> | 
<a href='http://blogadress/soggetto/parent2'>parent2</a> » <a href='http://blogadress/soggetto/child2'>child2</a> |</p>
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.