Support

Account

Home Forums General Issues How to display taxonomy checkbox links on WooCommerce product category archive

Solved

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.

    Thanks

  • Hi @cjf9

    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.

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

The topic ‘How to display taxonomy checkbox links on WooCommerce product category archive’ is closed to new replies.