1

I'm using the following code to display a custom taxonomy in the metadata for a single post, but now I need to display multiple terms separated by commas instead of just a single term. Just not sure how to do that. Is there a way to use something like wp_sprintf_l in combination with get_the_terms() to accomplish this? Or any other way?

Here's my current function:

$sources = get_the_terms( $post->ID, 'source' );
if ( ! empty( $sources ) && ! is_wp_error( $sources ) ){
foreach ( $sources as $source ) {
    $sourcename = $source->name;    
    }
$sourcelink = get_term_link( $source );
$sep2 = __( ',' );
}

Then I display it with this:

echo "<span class='source-meta'><a href='$sourcelink'>$sourcename</a>$sep2</span>;
1
  • 1
    you are looping all the terms in the foreach loop, but you are not using any apart from the last one. just build the echo into the foreach loop...
    – Michael
    Apr 16, 2020 at 1:49

1 Answer 1

1

I'm going to go and test this somewhere so I may need to make edits, but I think this should do it for you... as Michael mentioned in the comments, you have to wrap the output in the foreach. The method below will place a comma after each one, so I wrap the separator commas in their own span and then use CSS to hide the last one.

$sources = get_the_terms( $post->ID, 'source' );
if( !empty( $sources ) && !is_wp_error( $sources ) ) {
    echo '<span class="source-meta">';
    foreach( $sources as $source ) {
        $source_link = sprintf(
            '<a href="%1$s">%2$s</a>%3$s',
            esc_url( get_term_link( $source ) ),
            esc_html( $source->name ),
            '<span class="sep">, </span>'
        ); 
        echo sprintf( esc_html__( '%s', 'textdomain' ), $source_link );   
    }
    echo '</span>';
}

In your stylesheet (style.css) you then want the following:

.source-meta .sep:last-of-type{
     display:none;
}
8
  • Thanks! This worked. Just needed to edit 'category' and replace with 'source' in line 1. The one thing I'd like to have is "and" before the final term, like "source1, source2 and source3" rather than always the same separator. This is apparently what wp_sprintf_l is supposed to do, but I tried fiddling with it combined with your code and no luck yet. Any ideas how to get that final "and"?
    – Jon Fergus
    Apr 16, 2020 at 20:38
  • Sorry about that, I had to set it to 'category' when I tested on my site because I don't have a source taxonomy. Forgot to change it back. I'll take a look at the option to add and instead of the last comma. Apr 16, 2020 at 21:25
  • Can you try this and see if it works or throws an error: echo wp_sprintf_l( esc_html__( '%s, and %s' 'textdomain' ), $source_link ); Apr 16, 2020 at 21:32
  • Ya, it gives me an error: syntax error, unexpected ''textdomain'' (T_CONSTANT_ENCAPSED_STRING), expecting ')'
    – Jon Fergus
    Apr 17, 2020 at 4:02
  • Btw, if I use echo wp_sprintf_l( esc_html__( '%l', 'textdomain' ), $source_link ); it produces the same results as echo sprintf( esc_html__( '%s', 'textdomain' ), $source_link ); lists the items but only separated by commas. Just not sure why it's not using "and" in that case.
    – Jon Fergus
    Apr 17, 2020 at 4:11

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.