0

I am using the Advanced Custom Fields plugin, and have 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" provided by the ACF plugin (which allows you to choose specific categories within the product category taxonomy to be displayed on the front-end).

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; multiple simple text links.

The unsuccessful code I am using for this is:

$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 believe this is because I am missing some logic required for this information to be printed within the product category taxonomy of WooCommerce.

In order to get the other custom fields displaying on the product category archive template I had to use the following code, which is a bit different than how you normally display custom fields with ACF.

$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 believe I somehow need to include $taxonomy and $term_id in the first block of code I included above, but I just am not sure how to write it.

1 Answer 1

2

The issue has been solved. See below for the correct code to display ACF multiple taxonomy links on WooCommerce product category archives.

Specifically note the string: $related_terms = get_field('quick_links', $taxonomy . '_' . $term_id);

$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; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.