Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Have you saved any images to the post with the field in place? Have you provided the proper meta key to the function, which should be the id field in the add_field() declaration.

    Thread Starter mlyczko

    (@mlyczko)

    Hello

    I have images in the CPT – room.

    // Images for Slider
        $cmb->add_field( array(
            'name'       => __( 'Images for Slider', 'PixelEmu' ),
            'desc'       => __( 'Add images for slider.', 'PixelEmu' ),
            'id'         => $prefix . 'slider_images',
            'type'       => 'file_list',
            'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
            'repeatable'      => true,
        ) );

    Ican display images this way:

    $files = get_post_meta( get_the_ID(), 'pe_slider_images', 1 );
    							$img_size = 'medium';
    						    echo '<div class="file-list-wrap">';
    						    // Loop through them and output an image
    						    foreach ( $files as $key => $value ) {
    						        echo '<div class="file-list-image">';
    						        echo wp_get_attachment_image( key($value), $img_size );
    						        echo '</div>';
    
    						    }
    						    echo '</div>';

    Best Regards
    ML

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Based on the details provided, if you were to copy/paste the sample function back into your theme, you’d use it as follows:

    <div class="list_wrapper">
    cmb2_output_file_list( 'pe_slider_images', 'medium' );
    </div>
    Thread Starter mlyczko

    (@mlyczko)

    Hello

    I have in functions.php

    function cmb2_output_file_list( $file_list_meta_key, $img_size = 'medium' ) {
    
        // Get the list of files
        $files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );
    
        echo '<div class="file-list-wrap">';
        // Loop through them and output an image
        foreach ( (array) $files as $attachment_id => $attachment_url ) {
            echo '<div class="file-list-image">';
            echo wp_get_attachment_image( $attachment_id, $img_size );
            echo '</div>';
        }
        echo '</div>';
    }

    In my single.php i have:
    <?php cmb2_output_file_list( 'wiki_test_file_list', 'small' ); ?>

    In my CPT that im checking i have loaded images: https://screencast.com/t/xTqWUA4C8

    I still cant get the images, only empty divs. What can be wrong in my code?

    Best Regards
    ML

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The only way I can see this not working, is if you are either not inside the loop, or the post in question doesn’t have any images attached to it. It’s all reliant on the $files variable finding data to loop over.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘File List display’ is closed to new replies.