CPT with taxonomy, custom field with image attached to taxonomy
Support › MB Term Meta › CPT with taxonomy, custom field with image attached to taxonomyResolved
- This topic has 5 replies, 3 voices, and was last updated 5 months ago by
dwcouch.
-
AuthorPosts
-
October 4, 2022 at 6:00 PM #38530
Joe
ParticipantHello,
I have a CPT called Pack (slug label), I have setup a taxonomy called Label (slug label), the taxonomy is attached to the CPT Pack(pack) and Media(attachment) via the UI of Metabox (Tab taxonomy).
Then I setup a custom field using the UI Tab Custom Fields. The field group ID is 'taxonomy_add', the field type is 'image advanced', the ID is 'label_image_upload', max number of file '1', ticked 'show as admin column', in the settings I have picked the Location 'Taxonomy' and Label(label).
With this setup it is possible to upload one image to the taxonomy 'label', the uploaded image is displayed in the admin edit screen of the taxonomy 'label'. so far so good !
CPT : pack
taxonomy : 'label' is attached to CPT 'pack' and Media
field group of custom field : taxonomy_add is attached to taxonomy 'label'
ID custom field : label_image_uploadI am using the oxygen page builder, the following code works halfway in the repeater (in a codeblock)
the first 4 attached images to the CPT 'pack' are shown (those images have nothing to do with the taxonomy 'label', those were attached via the admin media screen).I have written in between the code, can you pls pls help me with this ?
<?php if($images = get_children(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => 4, // show all 'post_status' => 'published', 'post_mime_type' => 'image', 'order' => 'asc' ))) { foreach($images as $image) { echo wp_get_attachment_image($image->ID, array('70','90')); }} // WORKS GOOD UNTIL HERE - 4 Attachments are shown // FROM HERE ON, I WANT TO CRY :-( //Getting the term by name $terms = get_the_terms($post->ID, 'taxonomy_add'); // used print_r and var_dump with $terms -> invalid taxonomy and additional_data:protected //Retrieving images from taxonomy by term id foreach($terms as $term) { $image_id = get_term_meta($term->term_id, 'label_image_upload', true); echo '<img src=' . $image_id . '>'; } ?>
October 4, 2022 at 9:42 PM #38532Joe
ParticipantSo far I have accomplished this
<?php //Getting the term by name $terms = get_the_terms($post->ID, 'label'); print_r ( $terms ); //Retrieving images from taxonomy by term id foreach($terms as $term) { echo $term->term_id; ?>
the print_r($terms) function gives me the following:
Array ( [0] => WP_Term Object ( [term_id] => 53 [name] => Dog Art [slug] => dog-art [term_group] => 0 [term_taxonomy_id] => 53 [taxonomy] => label [description] => [parent] => 0 [count] => 7 [filter] => raw ) ) 53
which is great because this is the label, that is attached to the CPT, the term_id is also correct
now I just need the image ...
How do I get to the image of the custom field ?October 4, 2022 at 10:26 PM #38536Long Nguyen
ModeratorHi Joe,
If you use the field type
image_advanced
, you also need to use a loop to iterate through an array of image IDs. If you just want to upload 1 image, you can use the fieldsingle_image
to simply get the image ID. Please read more on the documentation
https://docs.metabox.io/fields/image-advanced/#template-usage
https://docs.metabox.io/fields/single-image/#template-usageAnd this documentation to get the term meta
https://docs.metabox.io/extensions/mb-term-meta/October 4, 2022 at 11:04 PM #38537Joe
ParticipantHi,
just found out how it works, if you have a better solution, please let me know :
<?php //$terms = get_the_terms($post->ID, array('label', 'week', 'formate', 'pack'), $terms = get_the_terms($post->ID, array('label'), array( 'hide_empty' => true, 'posts_per_page' => null ) ); foreach($terms as $term) { $image_id = get_term_meta($term->term_id, 'label_image_upload', true); $image_url = wp_get_attachment_image_src($image_id, 'full'); ?> <img src="<?php echo $image_url[0]; ?>" width="<?php echo $image_url[1]; ?>" height="<?php echo $image_url[2]; ?>" /> <?php } ?>
October 5, 2022 at 10:23 AM #38543Long Nguyen
ModeratorHi,
The code is correct to display the term meta image on the frontend. It is the same as mentioned in the documentation https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value
November 13, 2024 at 10:50 PM #46929dwcouch
ParticipantI too am adding an image field to a custom taxonomy. The field winds up near the bottom of the edit screen when adding or editing a term within the taxonomy. Is there a way to add a sidebar or move the location to display the field after or before other native fields?
-
AuthorPosts
- You must be logged in to reply to this topic.