Support Forum
Support › MB Term Meta › Calling Featured Image for Custom TaxonomyResolved
I have a custom taxonomy "state" for a CPT "locations".
Each state has a featured image created with MB Term Meta. The Field Group ID is "thumbnail-for-states", the field ID for the image url is "state-thumbnail-url", and the field ID for the single image is "state-thumbnail-image".
All I want to do is call the images for each state into my template.
The code for this section looks like this:
<section id="states" class="site-content">
<h2>Explore By State</h2>
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 0;
$states = get_terms( array(
'taxonomy' => 'state',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => '-1',
'paged' => $paged,
));
?>
<div class="state-grid">
<?php if($states) {
foreach($states as $state) {
?>
<div class="stateGrid-item">
<?php
$state = sanitize_term( $state, 'state' );
$state_link = get_term_link( $state, 'state' );
?>
<div class="state-image">
<!-- This is where the image goes -->
</div>
<h3 class="state-name"><?php echo $state->name ?></h3>
<div><?php echo $state->count; ?> Posts</div>
<div class="state-link">
<a href="<?php echo esc_url( $state_link ) ?>" class="button button-hollow-alt">Explore</a>
</div>
</div>
<?php } } ?>
</div>
</section>
I've tried everything I can think of, attempted various bits of code found in the forum and tried modifying the code found in your tutorial about adding custom featured images to categories but nothing works.
I don't want to use MB Views to build it like you did in the the tutorial https://metabox.io/create-category-thumbnails-featured-images-wordpress/#more-30057, I just want to put the right code into my template.
How do I do it?
Hi,
To get the term meta, please follow this documentation
https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value
https://docs.metabox.io/fields/single-image/#template-usage
For your case, the code should be:
...
foreach( $states as $state ) {
$image = rwmb_meta( 'state-thumbnail-image', ['object_type' => 'term'], $state->term_id );
echo '<img src='. $image['url'] .' />';
}
...
Thank you, that worked perfectly! Now onto the next problem, I really can't wait until you get the taxonomy tutorial done!
I need to put the same image as well as a cover image on the top of the archive page, just like the last part of the categories tutorial.
The Field Group ID for the cover image is "cover-image-for-state", the field ID for the image url is "cover-image-url", and the field ID for the cover image is "state_cover_image".
I've tried modifying the code from that step of the tutorial as:
<?php
$state = get_terms( 'state' );
$background_image = get_term_meta( $state[0]->term_id, 'state_cover_image', true );
if ($background_image) {
$link_image = wp_get_attachment_image_src( $background_image, 'full' );
$link_image_source = $link_image[0];
}
else {
$link_image_source = get_term_meta( $state[0]->term_id, 'cover-image-url', true );
}
?>
and calling the image with:
<div id="cover" class="pageHeader cover-area">
<div class="cover-image">
<img class="thumbnail-cat" src="<?php echo $link_image_source ?>">
</div>
</div>
but that isn't working.
For absolutely unknown reasons, taking this code:
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 0;
$states = get_terms( array(
'taxonomy' => 'state',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => '-1',
'paged' => $paged,
'exclude' => array('1289','1290','1291','1292','1294','1295','1296','1298','1299','1300','1301','1302','1303','1304','1305','1306','1307','1308','1309','1310','1311','1312','1313','1314','1315','1316','1317','1318','1320','1321','1323','1324','1325','1326','1327','1319','1322'),
));
?>
from the previous page and cutting it down to this:
<?php
$state = get_terms( array(
'taxonomy' => 'state',
'exclude' => array('1296','1301'),
));
$background_image = get_term_meta( $state[0]->term_id, 'state_cover_image', true );
if ($background_image) {
$link_image = wp_get_attachment_image_src( $background_image, 'full' );
$link_image_source = $link_image[0];
}
else {
$link_image_source = get_term_meta( $state[0]->term_id, 'cover-image-url', true );
}
?>
does work. But if I take any part of the remaining exclude statement out it stops working. I don't understand that at all because I shouldn't need an exclude statement or an array on this page!
What am I doing wrong?
Oh, and it shows the exact same cover image on every page, which is not at all helpful.
Okay, I figured out my problem so that's solved, but I have another question that you may be able to help me with.
I have two custom post types that are related, "single-state" and "destination". I also have a custom taxonomy called "state" that is associated with the "Destination" CPT but not the "Single State" CPT.
Each single state post contains information about one single state, such as Arizona, California, Idaho, etc. Destination posts are about places within a state, such as favorite camping spots, good places to eat, etc.
Every "Destination" has one "state" taxonomy, so, for instance, a post called "Visiting Grand Canyon West" would have the taxonomy of "Arizona" because Grand Canyon West is in Arizona.
Now on the Single State Post for Arizona I want to show the number of posts that are in the "State" taxonomy of "Arizona".
So far what I've tried shows the post count for all of the states on every single state post, but I only want the count for the posts with the same "state" taxonomy as the single state post (destinations "tagged" Arizona posts on the single post about Arizona, destinations "tagged" California on the single post about California, etc.).
This is the code I'm using
<?php
$states = get_terms(
'state', // your custom taxonomy slug
array(
'hide_empty' => false,
));
foreach ( $states as $term ) {
echo $term->slug . ' - ' . $term->count . '<br>';
}
?>
I'm really hoping that I can do this without having to use dozens of if/else statements.
Hi,
But there is no connection between a single-state
post and a term of state
taxonomy. You can try to use the extension MB Relationships to create a connection between single-state
CPT and state
taxonomy.
Then on the single-state
post, you can get the relation state
term and count the post destination
associated.
Read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#terms