简体   繁体   中英

ACF WordPress. Echo field in repeater field if not empty

I use ACF repeater field and I try to echo link if it not empty. My code

<?php global $query_string;
        $frs = query_posts($query_string.'&page=myCustomPage'); ?>
        <?php $frslides=get_field('repeater_field_name'); ?>
        <div class="fr-slider-links">
        <?php foreach($frslides as $frslide){?>
            <a href="#"><?php echo $frslide['sub_field1']; ?></a>
        <? } ?>
        </div><!-- fr-slider-links -->
    <div class="fr-slider-wrapper">
        <div id='dial'>
            <button class='left' type='button'></button>
            <button class='right' type='button'></button>
            <div class='content'></div>
            <?php $i = 1; ?>
            <?php foreach($frslides as $frslide){?>
                <div class='fr-slide-item'>
                <div class='icon sl-icon-<?php echo $i; ?>'></div>
                <div class='dial-content'>
                    <h3><?php echo $frslide['sub_field2']; ?></h3>
                    <div class="slider-box cf">
                        <img src="<?php echo $frslide['sub_field3']; ?>" height="121" width="121" alt="">
                        <p><?php echo $frslide['sub_field4']; ?></p>
                        <?php // Setup the standard repeater loop.
                            if ( have_rows( 'repeater_field_name' ) ) : while ( have_rows( 'repeater_field_name' ) ) : the_row();
                                if ( $source_link = get_sub_field( 'link_to_audio' ) ) : ?>
                                <audio controls>
                                    <source src="<?php echo esc_url( $source_link ); ?>" type="audio/ogg; codecs=vorbis">
                                    <source src="<?php echo esc_url( $source_link ); ?>" type="audio/mpeg"> 
                                </audio>
                                <?php endif; 
                        endwhile; endif; ?> 
                    </div><!-- slider-box -->
                </div>
                </div><!-- item -->
                <?php $i++;?>
            <? } ?>
        </div><!-- dial -->
        <?php wp_reset_query();?>

What I doing wrong, help me please) May be the reason is 2 loops with same name? To be more clear: I have 1 repeater field with name repeater_field_name and 5 sub fields in row. Fifth sub field I need to use for 1 .fr-slide-item and display there audio player. In that row I give the link to audio file, for all other rows that sub field is empty. I need to display that one, no in all .fr-slide-item 's.

You can use the standard repeater field markup (for the most part) to do this.

For example:

<?php // Setup the standard repeater loop.
if ( have_rows( 'repeater_field_name' ) ) : while ( have_rows( 'repeater_field_name' ) ) : the_row();

    // Check if a link has been set and assign it to a variable.
    if ( $source_link = get_sub_field( 'link_field' ) ) : ?>

        <audio controls>
            <source src="<?php echo esc_url( $source_link ); ?> . . .
            . . . 
        </audio>

    <?php endif; 

endwhile; endif; ?> 

I do that with code:

<?php $frslides=get_field('repeater_field_name'); ?>
<?php foreach($frslides as $frslide){?>
     <?php if( $frslide['link_field'] ): ?>
    <audio controls class="fr-audio">
    <source src="<?php echo $frslide['link_field']; ?>" type="audio/ogg; codecs=vorbis">
    <source src="<?php echo $frslide['link_field']; ?>" type="audio/mpeg">
    </audio>
<?php endif; ?>
<? } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM