skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    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 :)


  2. 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">';
     
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search