【问题标题】:ACF radio button not returning value within flexible contentACF 单选按钮未在灵活内容中返回值
【发布时间】:2018-08-13 20:32:41
【问题描述】:

我正在使用ACF plugin for WordPress。

使用灵活内容字段,管理员可以添加名为“复制”的布局。

在此布局中,他们可以选择“网格类型”(四分之一、三分之一、一半、完整)。以下是它在 ACF 中的外观:

这是它在我的模板文件中的外观:

<?php
  // Begin layout builder
  if( have_rows('layout_builder') ):
  while ( have_rows('layout_builder') ) : the_row();
?>

  <?php

    // Begin layout - Copy
    if( get_row_layout() == 'copy' ):

    // Begin repeater
    if( have_rows('copy') ):

    // Grid
    echo '<div class="grid-wrap">';

    while ( have_rows('copy') ) : the_row();

  ?>

    <div class="<?php

      if ( get_sub_field('grid_type') == 'quarter' ) {
        echo 'quarter';
      }

      if ( get_sub_field('grid_type') == 'third' ) {
        echo 'third';
      }

      if ( get_sub_field('grid_type') == 'half' ) {
        echo 'half';
      }

      if ( get_sub_field('grid_type') == 'full' ) {
        echo 'full';
      }

    ?>">

      <?= get_sub_field('copy'); ?>

    </div>

  <?php
    // End repeater
    endwhile;

    // End grid
    echo '</div>';

    endif;

    // End layout
    endif;
  ?>

<?php // End layout builder
  endwhile;
  endif;
?>

我期待grid_type 字段在前端吐出一个类,但它是空的。

当我使用&lt;?php var_dump( get_sub_field('grid_type') == 'half' ); ?&gt; 测试该字段时,它返回bool(false)。

我在这里做错了什么?

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    您将子字段 grid_type 放在错误的位置,您必须在复制中继器循环之外执行此操作。这应该有效:

    <?php
      // Begin layout builder
      if( have_rows('layout_builder') ):
      while ( have_rows('layout_builder') ) : the_row();
    ?>
    
      <?php
    
        // Begin layout - Copy
        if( get_row_layout() == 'copy' ):
    
    
        $gridType = get_sub_field('grid_type');
    
        // Begin repeater
        if( have_rows('copy') ):
    
        // Grid
        echo '<div class="grid-wrap">';
    
        while ( have_rows('copy') ) : the_row();
    
      ?>
    
        <div class="<?php
    
          if ( $gridType == 'quarter' ) {
            echo 'quarter';
          }
    
          if ( $gridType == 'third' ) {
            echo 'third';
          }
    
          if ( $gridType == 'half' ) {
            echo 'half';
          }
    
          if (  $gridType == 'full' ) {
            echo 'full';
          }
    
        ?>">
    
          <?= get_sub_field('copy'); ?>
    
        </div>
    
      <?php
        // End repeater
        endwhile;
    
        // End grid
        echo '</div>';
    
        endif;
    
        // End layout
        endif;
      ?>
    
    <?php // End layout builder
      endwhile;
      endif;
    ?>
    

    而且我建议您将中继器的名称从副本更改为其他名称,因为您的布局具有相同的名称。

    【讨论】:

      猜你喜欢
      • 2018-05-31
      • 1970-01-01
      • 2023-03-29
      • 2018-09-07
      • 2021-10-27
      • 2020-07-10
      • 2017-01-07
      • 2016-04-07
      • 2013-12-29
      相关资源
      最近更新 更多