• Resolved dgissen

    (@dgissen)


    I have a simple question:

    Within this code that I use to get a term list<?php if (get_the_term_list( $post->ID, 'country' ) != null ) { ?> <div>Country: <?php echo get_the_term_list( $post->ID, 'country', '', ', ', '' ); ?></div><?php } ?>
    how can I incorporate the word “and” before the last term of the query?

    For example, instead of returning “Germany, England, France” for country, I want it to return “Germany, England, and France.”

    How do I do this? Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello there,

    Try this code:

    
    get_the_term_list( $post->ID, 'country', '<span class="countries">', ', ', '</span>' )
    

    Then apply the following CSS code to insert and to the end of country term.

    
    .countries a:last-child:before{
      content: 'and ';  
    }  
    

    Regards,
    Kharis

    try and replcae your code with:

    $term_list = get_the_term_list( $post->ID, 'country', '<div>Country: ', ', ', '</div>' );
    if ( $term_list != null ) { 
    	$search = ', ';
    	$pos = strrpos( $term_list, $search );
       	if( $pos !== false ) {
           	$term_list = substr_replace( $term_list, ', and ', $pos, strlen( $search ) );
       	}
    	echo $term_list; 
    }

    Hi @dgissen,

    Does @alchymyth code work now?

    • This reply was modified 8 years, 2 months ago by Chris Mok.
    Thread Starter dgissen

    (@dgissen)

    As always, @alchymyth’s solution works very well; I am trying to work with Kharis’ as well, and will repost when I get it working. Thank you again!

    I tried the solution suggested by Michael, it worked perfectly!

    Thread Starter dgissen

    (@dgissen)

    I noticed one grammatical issue with Michael’s solution. When one has two terms, it places a comma after the first term. Michael, is there an easy solution for this?

    I would avoid get_the_term_list() and go ‘up a level’ to get_the_terms() which returns a PHP array that makes it more flexible.

    Here’s what I’d do:

    $terms = get_the_terms( $post->ID, 'country' );
    
    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
        $links = array();
    
        foreach ( $terms as $term ) {
            $links[] = '<' . 'a href="' . get_term_link( $term ) . '">' . $term->name . '';
        }
    
        $last = array_pop( $links );
        $comma_group = implode( ',', $links );
        
        echo implode( ' and ', array( $comma_group, $last ) );
    }
    

    This gets the raw terms as an array, then it loops through them and constructs an HTML link for each one.

    Then it splits the list into two groups: the group to be separated by commas, and the last item.

    The first group is flattened into a comma separated list. If there is 0 or only 1 terms, no commas will appear.

    Then that flattened list is combined with the last item with an “and”. If the last item is the only item, then no “and” will appear.

    This should do what you’re after.

    EDIT: Forgive the weird concatenation on the link inside the foreach, but it was the only way I could stop the forum turning it into a real link.

    Thread Starter dgissen

    (@dgissen)

    Thank you; all resolved.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom taxonomies: inserting the word “and”’ is closed to new replies.