How to Show taxonomies terms custom field value using ACF plugin - wordpress

I have used Advance Custom Field (ACF) wordpress plugin to add custom filed on Woocommerce -> Product -> Categories form.
Right now i can't able to print that field value on categories page.
Please see this screen shot to know field slug name and other details -> http://nimb.ws/BsSiJO and help me to show that field value on categories page.
Thanks,
Ketan.

Display a field
<p><?php the_field('field_name', $term); ?></p>
Retrieve a field
<?php
$variable = get_field('field_name', 'product-cat_23');
// do something with $variable
?>
Finding the term related to the current post
<?php
// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'category');
// we will use the first term to load ACF data from
if( !empty($terms) ) {
$term = array_pop($terms);
$custom_field = get_field('category_image', $term );
// do something with $custom_field
}
?>

Related

WordPress: How do i get a custom field value from custom taxonomy?

I have a custom taxonomy "News categories". This custom taxonomy has a custom text field "Category info".
How do I get the value of that field "Category info" within the post loop?
If you saved the custom field in the options table, you could retrieve it like this:
$options = get_option( 'category_info' );
echo $options[$taxonomy->term_id];
Or if you are using ACF, you can retreive the custom field like this:
<?php
// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'category');
// we will use the first term to load ACF data from
if( !empty($terms) ) {
$term = array_pop($terms);
$custom_field = get_field('category_image', $term );
// do something with $custom_field
}
?>

get a field of wordpress custom taxonomy term

I have registered a new taxonomy and assigned a custom field (image) to its term (subcategory) using Advanced Custom Fields 4.4.0. Somehow I can't get it displayed on custom page template.
$term = get_query_var('term');
$term = get_term_by('slug', $term, 'gallery');
$termid = $term->term_id;
get_field('featured_image', $termid);
What am I doing wrong?
Try echoing 'get_field()' or store it in a variable then echo it.
$custom = get_field('featured_image', $termid);
echo $custom;
Your code is right. But you must echo or print to write to screen. Or you can using the_field(); function.

Can't retrieve custom taxonomy within multisite

I'm currently working on a Wordpress multisite. While I can retrieve custom post types from within child blogs by using switch_to_blog(1) I cannot retrieve any custom taxonomies this way.
For instance, this page on the main blog lists posts from the "employment" post type and shows the custom "location" category associated with it.
http://209.59.177.85/employment-opportunities/
This one is within a child blog, and the template code is after a call to switch_to_blog(1):
http://209.59.177.85/waterloo/careers/
Same code, but the custom post type shows, and the custom taxonomy does not. The page does not generate any errors. I can't even get a basic term list printed on the page for the taxonomy.
Has anyone done this before?
Thanks!
Here's the solution, in case anyone is looking. The terms can, of course, be accessed directly from the db:
<span class="date">Location: <?php
$jobid = get_the_ID();
$queryterms = "
SELECT *
FROM ".$table_prefix."terms terms, ".$table_prefix."term_taxonomy term_taxonomy, ".$table_prefix."term_relationships term_relationships
WHERE (terms.term_id = term_taxonomy.term_id AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id)
AND term_relationships.object_id = ".get_the_ID()."
";
$terms = $wpdb->get_results($queryterms, OBJECT);
if ( $terms != null ) {
foreach( $terms as $term ) {
echo "<a href='/location/";
print $term -> slug ;
echo "/'>";
print $term -> name ;
echo "</a>";
unset($term);
} } ?> | Posted on: <?php the_time('F j, Y'); ?></span>

how to retrieve custom fields on taxonomy page?

I am using the "advanced custom fields" plugin and need to have it so that a custom field is pulled in for the category pages. I can get these to come in on pages, but the category pages are giving me a lot of trouble... 'video' is the name of the custom field I want to pull in.
This is the code I am currently using:
<?php echo get_field('video', 'clear-creek'.$wp_query->queried_object->term-4); ?>
or just a standard version like this which works on the regualar pages...
<?php the_field('video'); ?>
but it's not working... can someone please help steer me in the right direction?
Thanks!
If you are on a category's archive page, you would use this:
<?php echo get_field('video', 'category_'.get_query_var('cat')) ?>
If you are on a custom taxonomy instead, you would use this:
<?php $queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
echo get_field('video', $taxonomy . '_' . $term_id); ?>
This will dynamically get the taxonomy's slug and ID, and build out your get_field based on that information.

link to custom taxonomy by id

Through a series of specific requirements, I find myself needing to link to a custom taxonomy category using its term id...
I've got this - which displays a link to all taxonomies - I wish to change it so it only displays a link to the taxonomy with the term id dynamically pulled from a custom field I'm using.
$taxonomy = 'event-categories';
$terms = get_terms($taxonomy);
if ($terms) {
foreach($terms as $term) {
echo '<li>' . $term->name .'</li>';
}
};
essentiall I need "link_to_taxonomy_category(x)" where x = term_id
Thanks
The function you are looking for is get_term_link. It takes either a term object, ID or slug and a taxonomy name and returns a URL to the term landing page.
As a side note hard coding the link as you have in the example above is fragile -- always keep your code as portable as possible. If the site is moved to a different domain, that link will break. WordPress has several functions that generate links dynamically based on the current installation environment. get_term_link is one example.
From the Codex:
$terms = get_terms('species');
echo '<ul>';
foreach ($terms as $term) {
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
If you have single term_id e.g: 10, custom taxonomy series then you can use the following code to get the taxonomy term link.
note : change 10 to your variable for term_id and 'series' to your taxonomy.
$term = get_term( 10, 'series' );
$term_link = get_term_link( $term );
echo 'View All';

Resources