Php ACF-根据中继器字段中的后端选择更改链接
我在我的网站上使用了许多高级自定义字段。一件特别的事情是员工简介页面。我有一个选择字段,员工可以在其中添加社交图标或电子邮件图标。中继器字段是“社交”字段,如果他们选择“添加行”,则有“社交频道”选择字段和“社交链接”测试字段。我目前的代码是:Php ACF-根据中继器字段中的后端选择更改链接,php,wordpress,foreach,repeater,advanced-custom-fields,Php,Wordpress,Foreach,Repeater,Advanced Custom Fields,我在我的网站上使用了许多高级自定义字段。一件特别的事情是员工简介页面。我有一个选择字段,员工可以在其中添加社交图标或电子邮件图标。中继器字段是“社交”字段,如果他们选择“添加行”,则有“社交频道”选择字段和“社交链接”测试字段。我目前的代码是: <?php if ( have_rows('social')): ?> <div class="staff-social">
<?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; ?>
如果用户从后端的“社交频道”下拉列表中选择“邮件”,我需要在我的锚定标记前加上“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 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; ?>
非常感谢你,乔!