Nick
Nick

Reputation: 936

Advanced Custom Fields foreach looping in opposite direction to WordPress

I've been racking my brain on this one.

Background:

The following code works, and is grabbing the correct taxonomy term information for the relevant WordPress post.

Problem:

ACF is outputting terms in order of newest to oldest. WordPress's $game->name; is outputting terms in order of oldest to newest.

This basically means that the tooltip is not matching the image from get_field('icon');

$games = get_the_terms(get_the_ID(), 'games')

foreach ($games as $game) {
    $term = array_pop($games);
    if ( get_field('icon', $term) ) {
        echo '<img src="' . get_field('icon', $term ) . '" alt="' . $game->name . '" data-placement="bottom" data-toggle="tooltip" title="' . $game->name . '" />';
    }
} 

I have so far tried:

Suggestions would be enormously appreciated.

Upvotes: 0

Views: 308

Answers (1)

Steve
Steve

Reputation: 20469

You appear to be deliberately doing this in your code by using array_pop??

Simply use the $game variable from your foreach loop:

$games = get_the_terms(get_the_ID(), 'games')

foreach ($games as $game) {
    //$term = array_pop($games);
    if ( get_field('icon', $game) ) {
        echo '<img src="' . get_field('icon', $game ) . '" alt="' . $game->name . '" data-placement="bottom" data-toggle="tooltip" title="' . $game->name . '" />';
    }
}

Upvotes: 1

Related Questions