I am building a client site which uses the category taxonomy to control the content displayed. I have added custom fields to each category and would like to display them on individual pages.
I can call the custom fields to the category archives but am struggling to get them into a loop. On the landing page I have a taxonomy field that selects the categories to display. I can get the category names to display using the loop below but have been unable to successfully use get_field or an array to call a custom field below the <h5> tags.
I have found a number of topics relating to the problem but nothing that quite recreates this issue.
<?php $terms = get_field('featured_topics'); if( $terms ): ?> <?php foreach( $terms as $term ): ?> <h5 class="text-center"><?php echo esc_html( $term->name ); ?></h5> <?php endforeach; ?> <?php endif; ?>
Any help or relevant articles gratefully received.
I am just a user so no super expert but have you set the Return of the field to the correct one?
When you configure the field you can select what ACF returns when get_field is called.
There are only a few choices so try each one.
$term->name implies an array needs to be returned…
Also, printing out what get_field returns (as an array) helps a lot to see what is returned.
Hope that helps.
Like so many problems, a weekend off and a fresh look solved things. My initial code must have had a error in it as fixing it turned out to be super simple:
<?php $terms = get_field('featured_topics'); if( $terms ): ?> <?php foreach( $terms as $term ): ?> <h5 class="text-center"><?php echo esc_html( $term->name ); ?></h5> <p><?php the_field('s_t_intro', $term); ?></p> <a href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?> blog posts</a> <?php endforeach; ?> <?php endif; ?>
Hopefully that might help someone in the future.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.