Taxonomy extra fields

Tax Meta Class – FAQ

About Two weeks ago i posted my Tax Meta Class and ever since then my email inbox is full with few questions about the usage of the class over and over so just to get that out there and free some space in my inbox I'm going to answer these questions here:

[dropcap]Q[/dropcap]How to get the term data on the front end?

The class comes with a few helper functions which work just like post meta functions
so is simple as this:

$saved_data = get_tax_meta($term_id,'text_field_id');
echo $saved_data;

[dropcap]Q[/dropcap]How to get the term ID?

If you are on a term page (category, tag, or custom) you can use this:

$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$term_id = $current_term->term_id;

If you know the term name, slug or id then you can use this:

$term = get_term_by( $field, $value, $taxonomy);
$term_id = $term->term_id;

Where $field is name, slug or id
$value is the actual term name, slug or id
and $taxonomy is the name of the taxonomy.

and if you are in a loop then you can use:

//make sure you change <strong>$taxonomy</strong> to the taxonomy name 
$terms = wp_get_post_terms( $post->ID, $taxonomy);
foreach ($terms as $term){
   $term_id = $term->term_id;
   //do you term meta stuff here
}

[dropcap]Q[/dropcap]How to get the Image?

The image field data is saved as an array of image url as `src` for quick access, and attachment id as `id` so to simlpy display the image you can use:

$saved_data = get_tax_meta($term_id,'image_field_id',true);
echo '<img src="'.$saved_data&#91;'src'&#93;.'">';

and to get the attachment id of the image use:

$saved_data = get_tax_meta($term_id,'image_field_id',true);
$attachment_id = $saved_data['id'];

[dropcap]Q[/dropcap]How to different fields for different taxonomies?

just set the pages field so the config array to the taxonomy you want for each instance of the meta class

$config = array(
  'id' => 'demo_meta_box',
  'title' => 'Demo Meta Box',
  'pages' => array('category'), //<- set this to the taxonomy you want.
  'context' => 'normal', 
  'fields' => array(),
  'local_images' => false,
  'use_with_theme' => false
);

[dropcap]Q[/dropcap]How to use in a theme?

just set the use_with_theme field of the config array to true for each instance of the meta class

$config = array(
'id' => 'demo_meta_box',
'title' => 'Demo Meta Box',
'pages' => array('category'),
'context' => 'normal',
'fields' => array(),
'local_images' => false,
'use_with_theme' => true // <- make sure you set this to true if in a theme or a custom folder ); [/php] That is it for now but keep asking and i'll be happy to help and add your questions.

Incoming search terms:

Ohad Raz

WordPress Consultant, a WordPress Developer and a WordPress Freelancer With over 10 years experience in architecting web sites and applications. WordPress Development moderator and somethimes Plugin Developer.