Support Forum
Hi,
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?
Hi 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.
Hi,
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
Hi,
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/kpu57mp7
yes, 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 } ?>
To 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.
Hi,
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
Hi,
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 ID media_Frase2
to the function rwmb_meta()
.
For more information, please follow the documentation https://docs.metabox.io/rwmb-meta/#arguments.