Aleš Sýkora / April 5, 2022 / 4 comments

Get ACF taxonomy field for current post in Query

Post summary: For a situation where you need to display ACF taxonomy field for current terms assigned to the post in the WP Query, you may need the code below. Why to use ACF taxonomy field? It’s easy. I am using this for SEO purposes. How? I am creating website for language school. My taxonomy is called…

For a situation where you need to display ACF taxonomy field for current terms assigned to the post in the WP Query, you may need the code below.

Why to use ACF taxonomy field?

It’s easy. I am using this for SEO purposes. How? I am creating website for language school. My taxonomy is called “languages”. My terms are “English, Czech, Italian….” but I want to create internal links with anchor texts “English lessons, Czech online lessons, Italian online language courses” etc. Each one is unique based on keyword research.

That’s why I create ACF term field and use it like this. The main purpose for me is internal linkbuilding.

Get ACF term fields for current post in WordPress query

  1. Get global $post variables from current post in the query loop
  2. Get terms assigned to the post from your-taxonomy
  3. If not empty, then it will save your Tax field to the $catfield
  4. (optional) It also save link, name and slug, maybe you will need it
  5. it displays the $catfield for each assigned Term from your-taxonomy
<?php 
global $post;
    $terms = get_the_terms( $post->ID, 'your-taxonomy' );
    if ( !empty( $terms ) ) {
foreach ($terms as $term) {
  $catfield = get_field('text_pro_reference', $term);
  $catlink = get_term_link( $term );
  $catname = $term->name;
  $catslug = $term->slug;
?>
<?=$catfield?>
<?php
}}
?>

If you want to use link to the term archive:

<?php 
global $post;
$terms = get_the_terms( $post->ID, 'your-taxonomy' );
if ( !empty( $terms ) ) {
    foreach ($terms as $term) {
        $catfield = get_field('text_pro_reference', $term);
        $catlink = get_term_link( $term );
        $catname = $term->name;
        $catslug = $term->slug;
?>
        <a href="<?= $catlink ?>"><?= $catname ?></a>
        <?= $catfield ?>
<?php
    }
}
?>

Get ACF image term field

If you need to display ACF image field, just add this code:

	$catimg = get_field('category_image', $term);
	$size = 'full'; // (thumbnail, medium, large, full or custom size)

    	echo wp_get_attachment_image( $catimg, $size );

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

4 comments

  • Thank god i found this post.

    Need some help i hope i can get it from you.

    I have created a custom taxonomy for books called “publisher”, i need to display it on my product page (wich i have acomplished with the example you provide to the other user), but i also need to link the name of the publisher to an archive where i can see all books by the same publisher.

    That link is already generated when i createed the taxonomy (mypage.com/publisher/name-of-publisher) i just need to link it.

    Thannks for your help

  • Hi, great post! I just wondered if you could offer any advice on how I would use this to collect ALL taxonomy terms rather than the ones for the current post, so I could use this outside of the loop. Thanks…

  • A

    No worries, I gotchu. I have added another code to the article which is right for you.

  • A

    Hello David, so you mean all terms from some custom taxonomy anywhere?

    
    
    // get all terms from custom taxonomy "bus_types"
    $terms = get_terms( array(
        'taxonomy' => 'bus_types',
        'hide_empty' => false, // Zobrazit i prázdné termíny
    ) );
    
    // If any terms are available, we will create a list
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        echo '
      '; foreach ( $terms as $term ) { // Get term link $term_link = get_term_link( $term ); // display term name and link echo '
    • ' . esc_html( $term->name ) . '
    • '; } echo '
    '; }

Share Your Thoughts