Aleš Sýkora / April 5, 2022 / 4 comments
Get ACF taxonomy field for current post in Query
2 min read / ACF, Oxygen Builder, Plugins, WordPress / Share on: Twitter, LinkedIn, Facebook
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
- Get global $post variables from current post in the query loop
- Get terms assigned to the post from your-taxonomy
- If not empty, then it will save your Tax field to the $catfield
- (optional) It also save link, name and slug, maybe you will need it
- 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. 🍻
Manny
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
David
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…
Aleš Sýkora
No worries, I gotchu. I have added another code to the article which is right for you.
Aleš Sýkora
Hello David, so you mean all terms from some custom taxonomy anywhere?