Home › Forums › General Issues › How to display taxonomy checkbox links on WooCommerce product category archive
I have been at this for two days with no luck and I hoping someone can help. What I am trying to accomplish is very simple, yet I am missing something.
I have a several custom fields that appear on the WooCommerce product category edit screen (where you add/edit product categories), which then display the output on the archive-product.php category template. I have managed to display all of my custom fields on the front-end product category archives with the exception of the taxonomy custom field.
What I am trying to do is have the user choose from multiple product categories, then on the front-end just display the links to other product categories, a simple text link.
The unsuccessful code I am using for this is:
<?php $terms = get_field('quick_links'); if( $terms ): ?> <ul> <?php foreach( $terms as $term ): ?> <a href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a> <?php endforeach; ?> </ul> <?php endif; ?>
I tested the above code out on a regular page template and it works as I wanted. However, I cannot get it to display on my product category archive template.
I only was able to display the OTHER custom fields I have, with the following code:
<?php $queried_object = get_queried_object(); $taxonomy = $queried_object->taxonomy; $term_id = $queried_object->term_id; echo '<div class="top-content-block">'; the_field('top_content_block', $taxonomy . '_' . $term_id); echo '</div>'; echo '<div class="center-content-block">'; the_field('center_content_block', $taxonomy . '_' . $term_id); echo '</div>'; echo '<div class="featured-product-block">'; the_field('featured_products', $taxonomy . '_' . $term_id); echo '</div>'; ?>
Does anyone have any suggestions? I am at a loss.
You need to do the same for the tax field as with all your others and set the second parameter the same way.
<?php $queried_object = get_queried_object(); $taxonomy = $queried_object->taxonomy; $term_id = $queried_object->term_id; $related_terms = get_field('quick_links', $taxonomy . '_' . $term_id); if( $related_terms ): ?> <ul> <?php foreach( $related_terms as $related_term ): ?> <a href="<?php echo get_term_link( $related_term ); ?>"><?php echo $related_term->name; ?></a> <?php endforeach; ?> </ul> <?php endif; ?>
Thank you Johnathan! That did the trick! I was so close to the code you just posted, just wasn’t quite there yet.
No worries mate!
Glad I could help out.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.