0

I created a custom taxonomy called Newsroom Categories and was able to get the first term to display using some array values, conditional statements, and foreach loops:

<?php 
$args = array( 'public' => true, '_builtin' => false, 'name' => 'newsroom_categories' );
$output = 'names';
$operator = 'and';
$taxonomies = get_taxonomies($args,$output,$operator); 

if  ($taxonomies) {
    foreach ($taxonomies  as $taxonomy ) {
        $terms = get_terms($taxonomy);
        $count = count($terms);

        if ( $count > 0 ){
            foreach ( $terms as $term ) {
                $termlinks = get_term_link($term,$taxonomy);                                                
            }
        }
    }
}?> 

<a href="<?php echo $termlinks;?>"><?php echo $term->name;?></a> 

However no matter what term is selected in the page editor, only the first term is the only one that renders. How do I get more than the first term to display? Any input would be greatly appreciated.

1 Answer 1

1

Inside your foreach loop you're overwriting the value of $termlinks before you ever output the previous one:

foreach ( $terms as $term ) {
    $termlinks = get_term_link($term,$taxonomy);                                                
}

All that code will do is ensure that $termlinks is set to the last term in the $terms array.

It also means that when you finally output $term->name it will be the name of the last $term in the loops.

If you want to output for each of the terms you need to move the output to inside the loop:

foreach ( $terms as $term ) {
    echo '<a href="' . $termlinks . '">' . $term->name . '</a>';                                                
}
0

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.