Skip to content
lionel-bijaoui edited this page May 6, 2015 · 1 revision

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;

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
}

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['src'].'">';

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'];

How to use 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
);

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
);

Or use a custom folder :

$config = array(
	'id' => 'demo_meta_box',
	'title' => 'Demo Meta Box',
	'pages' => array('category'),
	'context' => 'normal', 
	'fields' => array(),
	'local_images' => false,
	'use_with_theme' => get_template_directory_uri() . '/Tax-meta-class' // <- a custom folder inside the theme
);