ACF taxonomy field in portfolio page

Hi all

as the object says, I’m using ACF fields in a portfolio page layout.

  • I have a TAXONOMY field with checkboxes to select the item category
  • Actually I’m trying to show multiple categories (2, for example)
  • I have my child theme
  • I’m working on the framework/views/integrity/content-portfolio.php file
  • I found this Working with array values
  • And this, more specific Display multiple values

If I use this (from the first of the two links)

<?php

$values = get_field('field_name');
if($values)
{
	echo '<ul>';

	foreach($values as $value)
	{
		echo '<li>' . $value . '</li>';
	}

	echo '</ul>';
}

// always good to see exactly what you are working with
var_dump($values);

?>

I get a list of IDs (and I really can’t understand how to convert them to category names)

If I use the second link’s code

<?php 
$terms = get_field('taxonomy_field_name');
if( $terms ): ?>
    <ul>
    <?php foreach( $terms as $term ): ?>
        <h2><?php echo esc_html( $term->name ); ?></h2>
        <p><?php echo esc_html( $term->description ); ?></p>
        <a href="<?php echo esc_url( get_term_link( $term ) ); ?>">View all '<?php echo esc_html( $term->name ); ?>' posts</a>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

I get nothing inside the h2 tags.

Any idea?

Hi @snowtura,

Thanks for writing to us.

Here get_field function is a predefined function in ACF plugin and returns the IDS as expected in the first set of code.
But in case the second reference of code get_field function has been used to get the terms, get_terms will be the correct function for this purpose.

You can learn more on get_terms from here : https://developer.wordpress.org/reference/functions/get_terms/

Hope it helps.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.