How do you display advanced custom fields on taxonomy terms? - php

In my archive page I added the following code:
<p><?php the_field('embed', $term); ?></p>
<p><?php the_field('download_output', $term); ?></p>
On the front-end when i'm on the archive page nothing is displayed. There are values in the field when looking at the WP-Admin taxo page.

Here is the documentation for ACF terms: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
And the term should be in this format: {$term->taxonomy}_{$term->term_id}

Here is an example to get value from taxonomy term
<?php
$taxonomy = get_query_var('taxonomy');
$termId = get_queried_object()->term_id;
the_field('Your_field_name', $taxonomy . '_' . $termId);
also you can find complete documentation for ACF Here
and you can use get_field if you want to store your value in variable then print it eg: $data = get_field('Your_field_name', $taxonomy . '_' . $termId); then echo the $data wherever you want

Related

How to show more than 1 custom taxonomy term or link on a custom post type?

I have created a custom taxonomy called 'tema' and the taxonomy has three terms. I want to show ALL the term links that are related to the current post. Currently I can only get my code to show ONE of the posts taxonomy terms...
I would like the term links displayed via my custom content.php file ("content-home.php") that I use for showing excerpts of my custom posts on my homepage.
Currently I have this code placed in my custom content.php file and it actually works fine but I can only get it to show ONE term:
<?php
$terms = get_the_terms( $post->ID, 'tema');
foreach($terms as $term) {
echo '<span>' . $term->name . '</span>';
}
?>
Can anyone please show me how I get it to show ALL the posts taxonomy term links?
In the WordPress Codex you can find:
For get_the_terms:
"Retrieve the terms of the taxonomy that are attached to the post."
http://codex.wordpress.org/Function_Reference/get_the_terms
For get_terms:
"Retrieve the terms in a taxonomy or list of taxonomies."
http://codex.wordpress.org/Function_Reference/get_terms
So, get_the_terms() will get the terms (e.g. categories) attached to a post, whereas get_terms() will retrieve the terms in a taxonomy (e.g. categories in the category taxonomy). For example, get_terms( 'category' ) will return all categories that you have added to your WordPress site.
You should use something like this:
<?php
$terms= get_terms(array('taxonomy'=>'tema'));
foreach($terms as $term){
echo '<span>' . $term->name . '</span>';
}
?>
Try below hook function to get list of taxonomy of particular post id,
//Returns All Term Items for "my_taxonomy"
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
print_r($term_list);
*my_taxonomy - replace your taxonomy
https://codex.wordpress.org/Function_Reference/wp_get_post_terms

Advanced custom fields displaying incorrectly on category pages. How do i limit to my taxonomy page only?

I added the code below to my archive page.
<?php
$taxonomy = get_query_var('taxonomy');
$termId = get_queried_object()->term_id;
the_field('embed', $taxonomy . '_' . $termId);
the_field('download_output', $taxonomy . '_' . $termId);
echo get_field('embed', $term);
echo get_field('download_output', $term);
?>
The archive page include taxonomy pages and category pages. The above code is displaying incorrectly on category pages. How do i limit the code above to only display on the taxonmy term "downloads"
I'm not exactly sure what you are trying to achieve but you can create taxonomy-downloads.php file in your theme and it will display only for this taxonomy

WordPress | Get term custom field

If I have custom field on Term custom field on Term Theme, its look like this:
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$myimage = get_field('image', $taxonomy . '_' . $term_id);?>
<?php echo $myimage; ?>
I want to display "image" custom filed on Term posts, I mean any post who use term X get the "image" of X.
How can I do that?
You need to change custom-field with the name of your custom field.
Like this
<?php echo get_post_meta($post->ID, 'term-theme', true); ?>
You can get more information from this WordPress codex page.

Show advanced custom field (ACF) if product content is empty

I created new ACF in Custom Fields panel and put this code in /woocommerce/templates/single-product/tabs/description.php:
...
<?php the_content(); ?>
//ACF code goes here:
<?php
$term = get_field('category');
if( $term ):
echo 'Category: ' . '' . $term->name . '.';
endif;
?>
It works very good, but if content product is empty, ACF doesn't shown in description tab, i.e. there is no description tab.
How can I make ACF to be shown if product content is empty?
Are you try pass the post ID ?
like
$val= get_field( "test", $ID);
The easiest way is to add space in product content.

Display Custom Taxonomy Field in sidebar

Hi Guys, I'm trying to display a taxonomy field called clpr_store_video in my Wordpress sidebar for each store is it possible to call the field value outside the loop on the sidebar, but yet attached to the post id?
I don't really know if its possible or how to do that.
EDITED
What I've tried from another tutorial but no luck.
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$subheading = get_field('clpr_store_video', $taxonomy . '_' . $term_id);
echo $subheading;
See code block to display taxonomy associated with post and list of taxonomies and their links:
If you wish to get taxonomy associated with particular post
$terms = get_the_terms( get_the_ID(), 'clpr_store_video' );
foreach ( $terms as $term ) {
echo $subheading = $term->name;
}
If you wish to get list and link of all taxonomies
<ul>
<?php
$taxonomies = get_terms('clpr_store_video', array("fields" => "all"));
foreach ($taxonomies as $taxonomy) {
echo '<li>'.$taxonomy->name.'</li>';
}
?>
</ul>

Categories