skip to Main Content

I’m trying to make a row layout where people can add an mp4 with ACF. So far so good, but when I try to add multiple video sections in the same post it outputs the same video in every player, even though they are different in the backend.

Does anyone know what I’m doing wrong?

Row layout:

<?php if (get_row_layout() == 'video') : ?>

    <?php get_template_part('template-parts/sections/section', 'video'); ?>

<?php endif; ?> 

Video section part

<div class="section section-type-video flex">

    <?php

    $video_mp4 =  get_field('video_file'); // MP4 Field Name
    $video_poster  = get_field('video_poster_image'); // Poster Image Field Name

    // Build the  Shortcode
    $attr =  array(
        'mp4'      => $video_mp4,
        'poster'   => $video_poster,
        'preload'  => 'auto'
    );


    echo wp_video_shortcode($attr);
     ?>

    
</div>

Many thanks in advance!

3

Answers


  1. Chosen as BEST ANSWER

    To answer my own question, I was targetting the wrong fields. Needed to target sub_fields instead of get_field.

    In ACF I set the return value of my subfield to array instead of url, to finish it I passed the url variable in the video shortcode array as an attribute.

    Correct code below:

    <div class="section section-type-video flex">
    
        <?php
    
        $video_mp4 =  get_sub_field('video_file'); // MP4 Field Name
        $video_poster  = get_sub_field('video_poster_image'); // Poster Image Field Name
        $video_url = $video_mp4['url'];
    
        // Build the  Shortcode
        $attr =  array(
            'mp4'      => $video_mp4,
            'src'      => $video_url,
            'poster'   => $video_poster,
            'preload'  => 'auto'
        );
    
        echo wp_video_shortcode($attr); ?>
        
    </div>
    

  2. please check the ACF Documentation for get_row_layout() how to display it

    Click here! to see it

    Login or Signup to reply.
  3. now i have made a Flexible Content field called "video-section"
    and created a layout called "video-main’ which contains a URL field called
    "video-url"

    <?php if( have_rows('video-section') ): ?>
    <?php while( have_rows('video-section') ): the_row(); ?>
        <?PHP 
    
       //get the URL 
        if( get_row_layout() == 'video-main' ): ?>
            <?php echo the_sub_field('video-url'); ?>
    
            <?php endif; ?>
            <?php endwhile; ?>
        <?php endif; ?>
    

    I used this code to echo all different URLs that included in the post that I was created for testing try it if that what you want to do

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search