【问题标题】:ACF - change link based on backend select choice in a repeater fieldACF - 根据转发器字段中的后端选择选项更改链接
【发布时间】:2015-08-02 05:19:43
【问题描述】:

我在我的网站上使用高级自定义字段来处理许多事情。特别是对于员工个人资料页面。我有一个选择字段,员工可以在其中添加社交图标或电子邮件图标。转发器字段是“社交”,如果他们选择“添加一行”,则有“社交频道”选择字段和“社交链接”测试字段。我目前的代码是这样的:

                <?php if ( have_rows('social')): ?>
                    <div class="staff-social">
                        <?php while ( have_rows('social')) : the_row() ?>
                            <li><a href="<?= the_sub_field('link'); ?>"><img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" /></a></li>
                        <?php endwhile; ?>
                    </div><!--end staff-social-->
                <?php endif; ?>

如果用户从后端的“social_channel”下拉菜单中选择“mail”,我需要在我的锚标记前添加一个“mailto:”。我试过这样做:

                       <?php while ( have_rows('social')) : the_row() ?>
                            <li>
                            <?php $select = get_sub_field_object('social_channel');
                            $choices = $select['choices']; 
                            foreach ($choices as $choice) {
                                if ($choice == 'mail') {
                                    echo '<a href="mailto:'.the_sub_field('link').'">';
                                } else echo '<a href="'.the_sub_field('link').'">';
                            } ?>
                            <img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" />
                            </a>
                            </li>
                        <?php endwhile; ?>

但这当然会为所有选择吐出一些东西,无论它们是否由用户在后端选择。谁能帮我这个?我认为这是非常基本的 PHP,但我不知道该怎么做。任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    您的 Select 字段应该只返回一个字符串,而不是一个数组,(确保您将 'social_channel' 字段设置为不允许多个值) 所以将您的代码更改为:

    <?php while ( have_rows('social')) : the_row() ?>
            <li>
                <?php $select = get_sub_field('social_channel');
                if($select == 'mail'){ 
                    $linkURL = 'mailto:'.get_sub_field('link');
                }else{
                    $linkURL = get_sub_field('link');
                } ?>
                <a href="<?php echo $linkURL; ?>"><img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" /></a>
            </li>
    <?php endwhile; ?>
    

    【讨论】:

    • 谢谢你一百万次,乔!
    猜你喜欢
    • 2021-07-24
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多