【问题标题】:ACF Group Sub Field Not DisplayingACF 组子字段不显示
【发布时间】:2019-11-16 15:51:54
【问题描述】:

我通过 ACF 选项页面创建了自定义字段,用户将在其中输入他们的社交媒体个人资料链接。如果没有链接,则不会在前端显示该图标。我通过创建一个名为“社交媒体”的组来实现这一点,该组将各种平台作为该组中的字段。

我正在努力将这些子字段值拉到主题页脚中。它根本不显示任何内容,但是当我显示未嵌套在组中的值时,它会显示出来。

Footer.php ↴

<?php
   if( have_rows('social_media') ):
   while( have_rows('social_media') ) : the_row();
?>
<li>
   <a href="<?php the_sub_field('instagram'); ?>">
      <img src="<?php bloginfo('template_directory'); ?>/assets/img/icons/instagram.png" width="25px" height="25px" alt="Instagram Profile">
   </a>
</li>
<?php endwhile; endif; ?>

ACF 集团↴

ACF 选项页面↴

TIA 寻求帮助。非常感谢。

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我假设这些字段位于 ACF 选项页面上,因此您必须在 have_rows 函数中传递字符串 options

    have_rows('social_media', 'options')
    

    查看此链接了解更多信息https://www.advancedcustomfields.com/resources/options-page/

    代码如下:

    <?php
       if( have_rows('social_media', 'option') ):
       while( have_rows('social_media', 'option') ) : the_row();
    ?>
    <li>
       <a href="<?php the_sub_field('instagram'); ?>">
          <img src="<?php bloginfo('template_directory'); ?>/assets/img/icons/instagram.png" width="25px" height="25px" alt="Instagram Profile">
       </a>
    </li>
    <?php endwhile; endif; ?>
    

    请注意,子字段不需要选项字符串

    当字段为空时隐藏 Li 的简洁方法:

    <?php while ( have_rows('social_media', 'option') ) : the_row(); ?>
        <?php if (get_sub_field('instagram')): ?>
            <li>
               <a href="<?php the_sub_field('instagram'); ?>">
                  <img src="<?php bloginfo('template_directory'); ?>/assets/img/icons/instagram.png" width="25px" height="25px" alt="Instagram Profile">
               </a>
            </li>
        <?php endif; ?>
    <?php endwhile; ?>
    

    【讨论】:

    • 这行得通。太感谢了!使用此代码,如果没有链接,if...while...else 语句隐藏 LI 的最佳方法是什么?
    • 不错!我用一个解决方案编辑了我的答案,以在字段为空时隐藏 Li
    【解决方案2】:

    如果您在选项页面中创建了此字段,则必须将option 参数与该字段一起传递。检查此链接https://www.advancedcustomfields.com/resources/options-page/

    这样

    <?php the_field('your-field-name', 'option'); ?>
    

    所以试试下面的代码。

    <?php
       if( have_rows('social_media', 'option') ):
       while( have_rows('social_media', 'option') ) : the_row();
    ?>
    <li>
       <a href="<?php the_sub_field('instagram'); ?>">
          <img src="<?php bloginfo('template_directory'); ?>/assets/img/icons/instagram.png" width="25px" height="25px" alt="Instagram Profile">
       </a>
    </li>
    <?php endwhile; endif; ?>
    

    【讨论】:

      【解决方案3】:

      当您从 ACF 选项页面回显字段时,只需传递 'option' 参数,例如 the_field('acf_field_name', 'option' )

      在您的情况下,代码应该是:

      <?php
      if( have_rows('social_media', 'option') ):
         while( have_rows('social_media', 'option') ) : the_row();
      ?>
      <li>
         <a href="<?php the_sub_field('instagram'); ?>">
            <img src="<?php bloginfo('template_directory'); ?>/assets/img/icons/instagram.png" width="25px" height="25px" alt="Instagram Profile">
         </a>
      </li>
      <?php endwhile; endif; ?>
      

      【讨论】:

        猜你喜欢
        • 2016-02-04
        • 2018-04-11
        • 1970-01-01
        • 1970-01-01
        • 2018-02-10
        • 1970-01-01
        • 2020-12-11
        • 2017-01-01
        • 1970-01-01
        相关资源
        最近更新 更多