Custom Fields
Re-Envisioned
Support General How to retrieve custom fields in attachment loop?

  • Creator
    Topic
  • #20449
    Resolved CarmineSasso
    Participant

    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
    Replies
  • #20450
    Long Nguyen
    Moderator

    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.

    #20451
    CarmineSasso
    Participant

    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

    #20453
    Long Nguyen
    Moderator

    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

    #20454
    CarmineSasso
    Participant

    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' =&gt; esc_html__( 'Descrizione Media', 'text-domain' ),
        'id' =&gt; 'descrizione-media',
        'post_types' =&gt; array(
            0 =&gt; 'attachment',
        ),
        'context' =&gt; 'normal',
        'priority' =&gt; 'high',
        'autosave' =&gt; true,
        'fields' =&gt; array(
            array (
                'id' =&gt; $prefix . 'Frase2',
                'type' =&gt; 'textarea',
                'name' =&gt; esc_html__( 'Frase', 'text-domain' ),
            ),
            array (
                'id' =&gt; $prefix . 'tag_autori',
                'type' =&gt; 'taxonomy',
                'name' =&gt; esc_html__( 'Autore', 'text-domain' ),
                'taxonomy' =&gt; 'autori',
                'field_type' =&gt; 'select',
            ),
            array (
                'id' =&gt; $prefix . 'tag_immagine',
                'type' =&gt; 'taxonomy',
                'name' =&gt; esc_html__( 'Tipologia di immagine', 'text-domain' ),
                'taxonomy' =&gt; 'tipologia-di-immagine',
                'field_type' =&gt; 'checkbox_list',
                'inline' =&gt; 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 } ?>

    #20455
    CarmineSasso
    Participant

    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.

    #20459
    CarmineSasso
    Participant

    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

    #20460
    Long Nguyen
    Moderator

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.