How to retrieve custom fields in attachment loop?
- This topic has 7 replies, 2 voices, and was last updated 4 years, 10 months ago by
Long Nguyen.
-
AuthorPosts
-
June 22, 2020 at 2:32 PM #20449
CarmineSasso
ParticipantHi,
I have an issue when I try to retrieve the custom fields configured in attachments.<?php
$images = rwmb_meta('images_select');
foreach ($images as $index => $image) {
?>
<figure>
" alt="<?php echo $image['alt']; ?>">
<figcaption><?php echo rwmb_meta("some-custom-field"); ?></figcaption>
</figure>
<?php } ?>I'm not able to print the value of the custom fields I have in an attachment.
Any suggestion on this topic?
June 22, 2020 at 2:53 PM #20450Long Nguyen
ModeratorHi Carmine,
Could you please share the code that you are using to create the field (images_select)? I will take a closer look and give a quick response.
June 22, 2020 at 3:09 PM #20451CarmineSasso
ParticipantHi,
I try to better explain my case.
I have a custom post type with a custom field (an image_advanced) and I want to print all the custom fields of all the images. This because I extended the media attachments with some custom fields.This is the code of the image_advanced. In the previous comment, I changed the id because of the Italian. this is the original code
array (
'id' => $prefix . 'select_immagini',
'type' => 'image_advanced',
'name' => esc_html__( 'Seleziona immagini', 'text-domain' ),
'max_file_uploads' => 30,
'max_status' => 1,
'add_to_wpseo_analysis' => true,
),Thank you
June 22, 2020 at 3:47 PM #20453Long Nguyen
ModeratorHi,
Your code shows the alt text of the image as well.
$select_immagini = rwmb_meta( 'select_immagini' ); foreach ($select_immagini as $index => $image) { echo $image['alt']; echo '<br>'; }
Could you please edit the image and check the alt text of the image again?
https://share.getcloudapp.com/kpu57mp7June 22, 2020 at 4:11 PM #20454CarmineSasso
Participantyes, I show the "standard" information alt, full_url ecc.
The issue is on the custom field added to the standard attachment. I attach the code$prefix = 'media_';
$meta_boxes[] = array ( 'title' => esc_html__( 'Descrizione Media', 'text-domain' ), 'id' => 'descrizione-media', 'post_types' => array( 0 => 'attachment', ), 'context' => 'normal', 'priority' => 'high', 'autosave' => true, 'fields' => array( array ( 'id' => $prefix . 'Frase2', 'type' => 'textarea', 'name' => esc_html__( 'Frase', 'text-domain' ), ), array ( 'id' => $prefix . 'tag_autori', 'type' => 'taxonomy', 'name' => esc_html__( 'Autore', 'text-domain' ), 'taxonomy' => 'autori', 'field_type' => 'select', ), array ( 'id' => $prefix . 'tag_immagine', 'type' => 'taxonomy', 'name' => esc_html__( 'Tipologia di immagine', 'text-domain' ), 'taxonomy' => 'tipologia-di-immagine', 'field_type' => 'checkbox_list', 'inline' => true, ), ), ); return $meta_boxes;
those fields are attached to the attachment type.
In my code what is not shown is the "Frase2" custom field
<?php
$images = rwmb_meta(‘select_immagini’);
foreach ($images as $index => $image) {
?>
<figure>
” alt=”<?php echo $image[‘alt’]; ?>”>
<figcaption><?php echo rwmb_meta(“Frase2”); ?></figcaption>
</figure>
<?php } ?>June 22, 2020 at 4:22 PM #20455CarmineSasso
ParticipantTo summary my issue.
I have a post with an image_advanced.
For each image (attachment), I have added some custom fields.
When I loop the image advanced, I want to show the custom fields of the attachment, but at the moment this feature seems to be impossible to do.June 22, 2020 at 8:11 PM #20459CarmineSasso
ParticipantHi,
starting from this official guide for image_advanced https://docs.metabox.io/fields/image-advanced/
how can I stamp the custom fields associated with the specific image?Thank you
June 22, 2020 at 9:05 PM #20460Long Nguyen
ModeratorHi,
You can use the function
rwmb_meta( $field_id, $args, $post_id )
and pass the argument$image['ID']
, the code should be$select_immagini = rwmb_meta( 'select_immagini' ); foreach ($select_immagini as $index => $image) { echo rwmb_meta( 'media_Frase2', '', $image['ID'] ); echo '<br>'; }
and please notice that the custom field ID is
$prefix . 'Frase2'
so you have to pass the real field IDmedia_Frase2
to the functionrwmb_meta()
.For more information, please follow the documentation https://docs.metabox.io/rwmb-meta/#arguments.
-
AuthorPosts
- You must be logged in to reply to this topic.