WordPress | Get term custom field - php

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.

Related

Get WooCommerce Product Attribute label name

Variable/variations in woocommerce products.
I can do this to get the attribute value from pa_size: <?php echo $product_variation->get_attributes()['pa_size']; ?> which is somewhere in /wp-admin/edit.php?post_type=product&page=product_attributes.
But how do I get the pa_size label (in this case: 'Size')? Have tried to fetch everything based on post_type, page and then the "term_group". But that won't work. I can see that this usually is visible per default, but this is a custom solution. Also in https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/single-product/product-attributes.php I can't see where they print the actual label, only the "Attribute child label and value". But not actual parent (pa_size => Size).
Have googled like a maniac for hours now.
To get the label name of a WooCommerce product attribute you will use one of the 2 following ways:
1) Using Woocommerce wc_attribute_label() dedicated function:
$taxonomy = 'pa_size';
$label_name = wc_attribute_label( $taxonomy );
2) Using wordPress get_taxonomy() function:
$taxonomy = 'pa_size';
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
In your case, you already know the taxonomy itself pa_size.
All product attributes are just taxonomies, with the pa_ prefix beforehand.
Then you will be able to print them using get_term_by()
it should look something like this:
<?php
$term = get_term_by('slug', $product_variation->get_attributes()['pa_size'], 'pa_size');
echo $term->name;
?>

How do you display advanced custom fields on taxonomy terms?

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

target a WooCommerce category's advanced custom field

I'm trying to get the advanced custom field for a WooCommerce category. With the following code I get the woocommerce categories:
$categories = get_terms('product_cat');
var_dump($categories);
But why isn't any ACF info included? Is there another function which do gets the ACF info?
UPDATE
This is outside the loop. So I'm trying to get a custom field of a specific product category. I found this info: http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
I can't get it to work.
Answer
To get the ACF with get_field(). I needed to use the cat_ID of the array I got with get_categories(). (Maybe it also works with get_terms())
I failed to grasp te second parameter in get_field()
I made it in the following way:
$id = 'product_cat_' . $category->cat_ID;
echo get_field ('field_name', $id);
Based on the documentation of ACF it appears you would get the custom term data as follows:
$category = get_term_by('slug', 'your-category', 'product_cat');
If( ! is_wp_error( $category ) && $custom_field = get_field('your_custom_field', $category ) ){
echo $custom_field;
}
// get the current taxonomy term
$term = get_queried_object();
// vars
$image = get_field('image', $term);
$color = get_field('color', $term);
see documentation here

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