I would suggest to use update_term_meta()
and get_term_meta()
as they were introduced in WordPress 4.4. This would help to keep your wp_options
table smaller.
But either way, you need to know the ID of the term in the “Frontend”.
Using Term meta you would need it:
$term_meta = get_term_meta( $term_id, 'series_images', true );
Using your solution you would need it:
$term_meta = get_option( "weekend-series_" . $t_id );
So the question is:
How do I get the ID of a term?
How to get the current term ID on the taxonomies page
A very useful function is get_queried_object()
, which returns the queried object. If you are in the taxonomy.php template, or the tag.php or the category.php this would be the current term object:
WP_Term Object
(
[term_id] => 20
[name] => Schlagwort
[slug] => schlagwort
[term_group] => 0
[term_taxonomy_id] => 20
[taxonomy] => post_tag
How to get custom field value in frontend for taxonomy =>
[parent] => 0
[count] => 0
[filter] => raw
)
So to get the terms ID in these templates you can do something like:
$current_object = get_queried_object();
$term_id = $current_object->term_id;
How do I get the term IDs of terms attached to a certain post?
If you want to display those images lets say in the single.php template, you would need to get the term IDs attached to the current post. With get_the_terms()
you can get exactly those. If you use it inside the loop* you can simply do something like this:
$terms = get_the_terms( get_the_ID(), 'post_tag' );
foreach ( $terms as $term ) {
$term_id = $term->term_id;
/* Do something with the $term_id */
}
The first parameter is the current post ID while the second parameter is the slug of the taxonomy (in my example its post_tag
for the tags). What you get in return is an array of Term Objects.
How to get the term IDs of all terms of a taxonomy
At last, lets say, you have a taxonomy and you want the term IDs of all the terms attached to this taxonomy get_terms()
is your friend.
$terms = get_terms( 'post_tag' );
foreach ( $terms as $term ) {
$term_id = $term->term_id;
/* Do something with the $term_id */
}