【发布时间】: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