ACF帖子对象获得术语链接

What's the simplest way to get the link of a custom post type term in wordpress using acf post object? I'm using acf field type => post object to display a subfield named 'product'. This 'product' subfield is a custom post type (let's say called apples). Apples has categories/terms called red apple, green apple, etc.

Let's say I have an item grouped under red apple, how do I get this item to link to the 'red apple' term/category page?

To link to this item's single page, I can do something like this which works

<?php $product = get_sub_field('product'); ?>  
<a href="<?php echo get_permalink($product->ID); ?>">Link</a>

How do I get it to link to the term/category page dynamically? In that way, if I have multiple items in different terms/categories, they can link to their correct term/category pages.

If I understand correctly:

  • You have defined a custom post type called "products"
  • You also defined a custom taxonomy associated to your "products" called "apples"
  • The terms of your taxonomy "apples" are : red, green, etc..
  • with

     $product = get_sub_field('product')
    

    in $product you have the object related to the post selected from the user

you can use

$post_terms_array=get_the_terms($product, 'apples');

to get in $post_terms_array all the terms related to the $product post.

Than for example you can get the slug of the first term associated to the $product with:

$post_terms_array[0]->slug

with this one you can construct your url for taxonomy archive page