【发布时间】:2018-06-16 08:44:49
【问题描述】:
我使用的是 ACF,内容灵活。
我为 facebook、twitter 等创建了媒体字段。
字段会得到链接,因为前端只是公司的标志。
我需要显示html,只有设置了值,否则不显示。
我怎样才能做到这一点?我需要这样的东西:
if( get_row_layout() == 'social_media_icons' ): ?>
if get_sub_field('media_facebook') is set, show this {
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_facebook'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
}
if get_sub_field('media_twitter') is set, show this {
<li>Twitter</li>
}
<?php endif; ?>
我试过这个:
<?php
while ( have_posts() ) : the_post();
if( have_rows('social_media','user_'. $author_id) ):
while ( have_rows('social_media', 'user_'. $author_id) ) : the_row();
if( get_row_layout() == 'social_media_icons' ): ?>
<?php if (get_sub_field('media_facebook')) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_facebook'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
<?php } ?>
<?php if (get_sub_field('media_twitter')) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_twitter'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
<?php } ?>
<?php if (get_sub_field('media_github')) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_github'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-github" aria-hidden="true"></i></a></li>
<?php } ?>
<?php if (get_sub_field('media_linkedin')) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_linkedin'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-linkedin-square" aria-hidden="true"></i></a></li>
<?php } ?>
<?php if (get_sub_field('media_google')) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_google'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-google-plus-official" aria-hidden="true"></i></a></li>
<?php } ?>
<?php if (get_sub_field('media_reddit')) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_reddit'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-reddit-alien" aria-hidden="true"></i></i></a></li>
<?php } ?>
<?php if (get_sub_field('media_personal_website')) { ?>
<li class="author-profile__social-item"><a rel="noopener noreferrer" href="<?php echo get_sub_field('media_personal_website'); ?>" class="author-profile__social-anchor" target="_blank"><i class="fa fa-globe" aria-hidden="true"></i></a></li>
<?php } ?>
<?php endif; ?>
<?php endwhile;
else :
echo 'no content';
endif;
endwhile; // End of the loop.
?>
但是,这是什么,但它每次显示 3 次。
所以它显示了 facebook、git、linke、facebook、git、linke、facebook、git、linke
它不显示未设置值的那些。
我怎样才能不让他们重复那些已经设置好的?
ACF 灵活内容
【问题讨论】:
-
您查看过您创建的 ACF 字段吗?检查的性质:` if (get_sub_field('media_facebook')) ` 应该完全为空才能为 FALSE。换句话说,如果您有 a = " ",a = TRUE,并且您的 if 语句将显示该字段。一种测试方法是检查字段中的内容是 var_dump("the_field('media_facebook')"),如果您有未定义以外的任何内容,即使您没有在页面上设置默认值,也会设置默认值.
-
唯一显示的是我填写的字段,没有填写的不显示。但是,填充的值会显示几次。就像在这个截图中一样 - i.imgur.com/0pyzjVW.png
标签: wordpress advanced-custom-fields acfpro