0

I am trying to display a list of event producers, which are stored as a taxonomy. For each event (custom post type) there can be anywhere between 1 and many producers, so I would like to list them, comma separated, with a "&" between the second to last one and last one. The below function does the job, except in the case of only having one producer. How would I make this work for 1 producer or many producers at the same time?

function the_producers($post_id = false) {
    if ($post_id == false) {
        $post_id = get_the_ID();
    }
    $producers = get_the_terms($post_id, 'producers');
            $prods = array();
    foreach ($producers as $producer) {
            $prods[] = $producer->name;
    }
    $str = array_pop($prods);
    if ($prods) {
        $str = implode(', ', $prods).' & '.$str;
        return $str;
    } else {
        return false;
    }   
}

I've tried counting the number of iterations and if there's only 1, just returning the single producer, but that doesn't seem to display anything for some reason:

function the_producers($post_id = false) {
    if ($post_id == false) {
        $post_id = get_the_ID();
    }
    $producers = get_the_terms($post_id, 'producers');
            $prods = array();
            $cnt = 0;
    foreach ($producers as $producer) {
            $cnt++;
            $prods[] = $producer->name;
    }
    if ($cnt == 1) {
        $str = $producer->name;
    } else {
        $str = array_pop($prods);
        if ($prods) {
            $str = implode(', ', $prods).' & '.$str;
            return $str;
        } else {
            return false;
        }
    }
}

2 Answers 2

1

Instead of counting inside your foreach loop, why not count the result from get_the_terms. Something like this

$q = count($producers);

From that, you can then use a conditional like

if( 1 == $q ) {
  // do something when only one term exists
}else{
  // do something for mutliple terms
}
2
  • Thanks for the info. It didn't solve my problem, but I like your method of counting better so I used it in my final solution, which turned out to be that I was not navigating the object(s) produced by get_the_terms() correctly. Working solution is below.
    – Eckstein
    Commented Sep 5, 2014 at 3:48
  • 1
    Indeed, there you go! It won't let me accept my own til tomorrow, but I'll come back and take care of it. Thanks again!
    – Eckstein
    Commented Sep 5, 2014 at 4:02
1

This is working. Pieter Gossen's method of counting is better, so I implemented that as well:

function the_producers($post_id = false) {
    if ($post_id == false) {
        $post_id = get_the_ID();
    }
    $producers = get_the_terms($post_id, 'producers');
    $q = count($producers);
    if( 1 == $q ) {
        $str = reset($producers)->name;
        return $str;
    } elseif ($q > 1) {
        $prods = array();
        foreach ($producers as $producer) {
        $prods[] = $producer->name;
        }
        $str = array_pop($prods);
        if ($prods) {
            $str = implode(', ', $prods).' & '.$str;
            return $str;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.