ACF – Get Custom field value from category

All the template functions (get_field, the_field, etc) can be used to load values from a taxonomy term, however, a second parameter is required to target the term. This is similar to passing through a $post_idparameter to target a specific post object.

The $post_id parameter needed is a string containing the the term’s taxonomy and ID in the following format; “{$term->taxonomy}_{$term->term_id}”

Instead of the above string, a term object can also be passed as the $post_id parameter as of ACF v4.3.3. You can get a term object via many of WP’s functions such as get_term.

Examples

Display a field

if you are on single product/post page

<!--?php $product_id = $post->ID;<br ?--> $taxonomy = 'product_cat'; // this is for any other custom taxonomy
$taxonomy = 'category'; // this is for default wordpress taxonomy
$terms = wp_get_post_terms( $product_id, $taxonomy );
?>

if you are on category page

<br /><?php

global $wp_query; // get the query object

$cat_obj = $wp_query->get_queried_object();

?>

<p><?php echo get_field('field_name',$cat_obj->taxonomy.'_'.$cat_obj->term_id); ?></p>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s