Get

Support General GetResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36105
    edujuanoxygen@gmail.com[email protected]
    Participant

    I have a custom field, called "Projects", located in a post type, called "photo", Projects has in the custom field an advanced images.

    In the post type, "Photos", I have created four posts, and within each post there are several images.
    https://i.ibb.co/PYdHz1b/for-Metabox-suport.jpg

    I need, with a loop, to access all the data of each image, like the ID of each image, the id of the post in which that image is, the name of the post, etc.

    Many Thanks

    #36109
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can follow the documentation to create a custom query to get all posts of a CPT and custom fields associated with each post.
    https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
    https://docs.metabox.io/fields/image-advanced/#template-usage

    For example:

    <?php
    
    $args = array(
        'post_type' => array( 'photo' )
    );
    // The Query
    $the_query = new WP_Query( $args );
     
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
            $images = rwmb_meta( 'my_field_id', ['size' => 'thumbnail'], get_the_ID() ); ?>
            <h3>Uploaded images</h3>
            <ul>
                <?php foreach ( $images as $image ) : ?>
                    <li><img src="<?= $image['url']; ?>"></li>
                <?php endforeach ?>
            </ul>
            <?php
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    #36115
    edujuanoxygen@gmail.com[email protected]
    Participant

    Many thanks, works perfectly.👍

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