How show media gallery | carbon fields wordpress

1k Views Asked by At

I using Carbon Fields in Wordpress. I have a problem, beacause I can not show media gallery. My code in functions.php

function crb_attach_post_meta_aboutus() {
    Container::make( 'post_meta', __( 'Singlebramy', 'single' ) )
        ->where( 'post_type', '=', 'dla-domu' )

        ->add_fields( array(
            Field::make( 'media_gallery', 'crb_media_gallery', 'Galeria' )
            ->set_type(  'image'  )
        ));
}

I tried use foreach but not work. Please help me.

2

There are 2 best solutions below

1
On

To display media gallery in Carbon fields:

$gallery  = carbon_get_post_meta( get_the_ID(), 'crb_media_gallery' );

foreach( $gallery  as $i => $image ){

  echo '<img src="'.wp_get_attach`enter code here`ment_url( $image ).'" class="d-block w-100">';
 
}
0
On

Ok, I found answer on my questions. I used foreach and wp_get_attachment_url(), below I added a code snippet with a solution to the puzzle.

foreach( $media_gallery as $i => $image ){
      if($i == 0){
            $next = 'active';
      }else{
         $next = '';
      }
      echo '<div class="carousel-item '.$next.'">';
      echo '<img src="'.wp_get_attachment_url( $image ).'" class="d-block w-100">';
      echo '</div>';
}

This is a snippet code with bootstrap carousel :)