【问题标题】:ACF WordPress. Echo field in repeater field if not emptyACF WordPress。转发器字段中的回显字段(如果不为空)
【发布时间】:2015-12-09 16:08:43
【问题描述】:

我使用 ACF 转发器字段,如果它不为空,我会尝试回显链接。 我的代码

<?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();?>

我做错了什么,请帮帮我)可能是2个同名循环的原因? 为了更清楚: 我有 1 个名称为 repeater_field_name 的转发器字段和 5 个子字段。我需要将第五个子字段用于 1 .fr-slide-item 并在那里显示音频播放器。在该行中,我给出了音频文件的链接,对于所有其他行,该子字段为空。我需要显示那个,没有.fr-slide-item's。

【问题讨论】:

  • 最后一个标签使用 PHP 短代码打开标签,可能无效。虽然不确定 WP 的东西。

标签: php wordpress custom-fields advanced-custom-fields


【解决方案1】:

您可以使用标准转发器字段标记(大部分情况下)来执行此操作。

例如:

<?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; ?> 

【讨论】:

  • 不幸的是,它没有按我需要的方式工作。现在,音频文件被添加到所有项目,而不是一个书面链接。我将更新我的问题并包含所有代码。
【解决方案2】:

我用代码做到这一点:

<?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; ?>
<? } ?>

【讨论】:

    猜你喜欢
    • 2016-06-17
    • 2017-02-22
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 2018-10-06
    • 1970-01-01
    相关资源
    最近更新 更多