【问题标题】:Hide row if ACF sub field is checked如果选中 ACF 子字段,则隐藏行
【发布时间】:2020-12-21 22:23:16
【问题描述】:

我得到了一个 ACF Reaper 字段,其中包含我试图显示的几行。但是,我只想要显示行,如果它选中了一个复选框(复选框是转发器中的一个子字段)。我试图通过使用 if in_array 来实现这一点,如“条件逻辑”下的ACF documentation 中所述:

if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )

我以 WordPress 简码输出结果。现在,我的代码有点工作,只是它显示了转发器字段中的所有结果(也包括那些未选中的结果)。我错过了什么??

我的代码:

function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');

if( $rows  ) {
    echo '<ul class="slides">';
    foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
        $image = $row['upload_dokument'];
        echo '<li>';
            echo get_field( 'upload_dokument' );
        echo '</li>';
        }
    }
    echo '</ul>';
}   

}

add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );

【问题讨论】:

    标签: php wordpress if-statement checkbox advanced-custom-fields


    【解决方案1】:

    在@maggiathor 的回答的帮助下设法解决了问题。由于某种原因,回声引起了问题。我不得不使用 return insted:

    function investor_bestyrelse_shortcode() {
                $rows = get_field('budgetter_og_nyhedsbreve');
        if( $rows  ) {
            $content = '<ul class="dokumenter">';
                foreach( $rows as $row ) {
                if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
                    $pdf = $row['upload_dokument'];
                    
                    $content = $content . '<li>' . $pdf . '</li>';
                }
                }
            }
            $content = $content . '</ul>';
            return $content;
        }   
    add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
    

    【讨论】:

      【解决方案2】:

      您不能在 foreach 循环中使用 get_sub_field(),您需要使用 have_rows-while-loop 或从关联数组中访问它:

      function investor_bestyrelse_shortcode() {
          $rows = get_field('budgetter_og_nyhedsbreve');
          if( $rows  ) {
              echo '<ul class="slides">';
                  foreach( $rows as $row ) {
                      if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
                      $image = $row['upload_dokument'];
                      echo '<li>';
                      echo $row['upload_dokument'];
                      echo '</li>';
                  }
              }
              echo '</ul>';
          }   
      }
      add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
      

      【讨论】:

      • 谢谢!这就说得通了。但是,仍然相同:显示转发器字段中的所有结果(也包括未选中的结果)。顺便提一句。代码中有语法错误。 add_shortcode() 末尾缺少分号;
      • 好的,你可以运行 var_dump($row);在循环开始时查看数组的样子,可能有错字或类似的东西。
      • 对 var_dump 不满意 - 不确定我是否做得对,因为它处于循环中。我也在阅读 ACF 中继器文档,发现他们没有使用 get_field 作为中继器字段,而是使用了 have_row(); - 也许我应该改变一下代码?文档:advancedcustomfields.com/resources/repeater
      • foreach 很好并且可以工作 - 另一个问题可能是短代码无法在您想要的帖子上运行。您可以检查 echo get_the_id();如果它是正确的职位。如果不是,您需要将 postId 传递到 get_field('key', $postid);功能。
      • 感谢您的所有帮助,@maggiathor。我确实在这个帖子中找到了解决方案并发布了答案:)
      猜你喜欢
      • 2019-01-04
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 2016-02-12
      • 2018-06-18
      • 2021-07-10
      • 1970-01-01
      相关资源
      最近更新 更多