• Resolved lucastosellolatini

    (@lucastosellolatini)


    Hello everyone! How are you?

    I always use cmb2 and I never had problems.
    The times I used a file list … I did it in a CPT. This time I need to do it in an option page.
    I manage to create the metabox in the backend and load the files and that they are saved there.

    My problem is when I want to show them in the frontend.

    I paste my code, which works correctly in the CPT. So they tell me where I’m wrong if they’re so kind.

    Backend:

        // Slider página principal
        $home_options->add_field( array(
            'name' => 'Slider página principal',
            'desc' => 'Agrega las imagenes del slider',
            'id'   => 'slider-principal-fox',
            'type' => 'file_list',
            'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
            'query_args' => array( 'type' => 'image' ), // Only images attachment
            // Optional, override default text strings
            'text' => array(
                'add_upload_files_text' => 'Añadir o Subir Imagen', // default: "Add or Upload Files"
                'remove_image_text' => 'Eliminar Imagen', // default: "Remove Image"
                'file_text' => 'Imagen:', // default: "File:"
                'file_download_text' => 'Descargar', // default: "Download"
                'remove_text' => 'Eliminar', // default: "Remove"
            ),
        ) );

    Frontend:

    /**
     * Sample template tag function for outputting a cmb2 file_list
     *
     * @param  string  $file_list_meta_key The field meta key. ('wiki_test_file_list')
     * @param  string  $img_size           Size of image to show
     */
    function cmb2_output_file_list( $file_list_meta_key, $img_size = 'large' ) {
    
    	// Get the list of files
    	$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );
    
    	// Loop through them and output an image
    	foreach ( (array) $files as $attachment_id => $attachment_url ) {
    
    		echo '<figure>';
    		echo wp_get_attachment_image( $attachment_id, $img_size );
    		echo '</figure>';
    	
    	}
    
    }
    
    cmb2_output_file_list( 'slider-principal-fox', 'large' );

    Thanks in advance.

    Regards!

    The page I need help with: [log in to see the link]

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

    (@tw2113)

    The BenchPresser

    The issue is that as an options page, it’s no longer getting stored as post meta. It’s being stored in the wp_options table, so your cmb2_output_file_list function needs to be duplicated or amended to use the options functions from WordPress.

    I would look over https://github.com/CMB2/CMB2-Snippet-Library/blob/master/options-and-settings-pages/theme-options-cmb.php as a general example, and specifically the function at the end named myprefix_get_option which can be used as a helper function in place of get_post_meta and such.

    If you’re confused by any parts of that, let us know and we can aim to explain those parts as best we can for you.

    Thread Starter lucastosellolatini

    (@lucastosellolatini)

    Michael, thank you very much for your quick response.
    As soon as I published the question, I realized that I only had to make a modification to the code I showed earlier.

    Don´t work

    // Get the list of files
    $files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );

    Work 🙂

    // Get the list of files
    $files = fox_get_home_option( 'slider-principal-fox', $file_list_meta_key, 1 );

    Likewise any advice to improve the code is welcome.

    Thanks again and regards.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Nothing obvious that i can see that’s an actual red flag, anything at this point that i’d suggest would be nitpicky and formatting/spacing.

    Glad to hear you got it worked out.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘File list of option page do not display in frontend’ is closed to new replies.