Support

Account

Home Forums Front-end Issues Displaying field with get_terms

Solving

Displaying field with get_terms

  • Hi

    I have a custom taxonomy with a custom field called thumbnail_image

    I want to create a page that lists all the taxonomies with a link, title and image. I can get it to display the term and link but when I try to add the ACF field I am having problems.

    <?php 
    					$terms = get_terms(
    						array(
    							'taxonomy'   => 'species',
    							'hide_empty' => false,
    						)
    					);					
    					if ( ! empty( $terms ) && is_array( $terms ) ) { ?>
    					<ul>					
    					<?php						
    					foreach ( $terms as $term ) { 
    						?>
    						<li>
    							<a href="<?php echo esc_url( get_term_link( $term ) ) ?>">						
    							<?php echo get_field('thumbnail_image', 'species_' . $term->term_id ) ?>		
    							<?php echo $term->name; ?>
    							</a>
    						</li>
    						<?php }	?>
    					</ul>
    				<?php } ?>

    I have tried

    $image = get_field('thumbnail_image', 'species_' . $term->term_id );
    and
    $image = get_field('thumbnail_image', $term);
    with
    <?php echo $image ?>

    and still the same problem.

    It just displays array. The PHP error is

    Notice: Array to string conversion in

  • Is this not possible yet?

  • Hey Wozzal, I was having the same issue and my work around for now was this was really close to your code:

    <?php // Get the taxonomy's terms
    $terms = get_terms(
        array(
            'taxonomy'   => 'news_categories',
            'hide_empty' => false,
        )
    );
    ?>
    <?php // Check if any term exists
        if ( ! empty( $terms ) && is_array( $terms ) ) {
            // Run a loop and print them all
            foreach ( $terms as $term ) { ?>
           
    
                <a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
                    <?php echo $term->name; ?>
                    <?php echo the_field( 'banner_text', $term ); ?>
                    <img src="<?php echo the_field( 'banner_image', $term ); ?>">
                </a>
    
    <?php       
            }
        } 
    ?>

    Getting the Term Name and URL was easy. Getting a simple field, like a “Text Field” from ACF was straight forward too using something like:

    <?php echo the_field( 'banner_text', $term ); ?>

    I really wanted to use an array for the image but that didn’t want to work so I opted for image URL instead as my work around and just echoed this with no var:

    <img src="<?php echo the_field( 'banner_image', $term ); ?>">

    If you did ever figure out how to do this with the Image Array please let me know.
    Thanks!
    Andy

  • The issue is that you have the image field set to return an array instead of a URL. See the documentation for the image field to see how to handle the different types of return values https://www.advancedcustomfields.com/resources/image/

  • Thanks John,

    I did try having the field set to Array to start with but was not having any luck with my usual markup.

    I then tested having the field set to URL and using the example above and that worked.

    For now this is fine for me but would like to eventually get the Array image coming through for things like alt text, etc.

    Cheers.

  • The problem is with the image field and would not be a problem with a text field. the_field() echos the field value, in this case an array that cannot be converted to these. If you return an array for an image field then you must read the elements of the array and create the output yourself as needed. This is covered in the documentation. ACF will not format the html for the image for you when the field is set to return an array. You will find that this is the case with most of the more advanced field types.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.