dongwenhui8900 2018-07-03 13:54 采纳率: 0%
浏览 45

ACF - 如果字段为空,如何不显示数据

this is my code (it is a bit messy, I'm sorry, I'm still a beginner). I'm using ACF (in WordPress), and what I'm trying to do is display certain data to certain logged-in users (for example: user1 has a table filled with different data compared to user2). If user1 has empty able - the table shouldn't be shown. That's why I'm checking the sub-field 'first'. If it's not empty, show all the following data that comes from the code. What am I doing wrong? Maybe I'm messing up with mixing PHP with HTML, I don't know. I'm faced with ACF and PHP for the first time.

I am grateful for any given help. Thank you in advance.

<?php 
$first = get_sub_field( 'first' );

if( ! empty( $first ) ) : ?>
    <h4>My heading</h4>
    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>First</th>
                    <th>Second</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if( have_rows( 'web_design_development', "$loggeduser" ) ):
                    while ( have_rows( 'web_design_development', "$loggeduser" ) ) : the_row(); ?>
                        <tr> 
                            <?php
                            $first = get_sub_field('first'); 
                            $second = get_sub_field('second');                                               
                            ?>
                            <td><?php echo $first; ?></td>
                            <td><?php echo $second; ?></td>            
                        </tr>
                    <?php endwhile; ?>
                <?php endif; ?> 
            </tbody>
        </table>
    </div>
<?php endif; ?>
  • 写回答

1条回答 默认 最新

  • dqqyp90576 2018-07-03 16:39
    关注

    To check if an ACF sub-field is empty, use get_sub_field combined with an if-statement.

    You should use the following:

    if ( get_sub_field( 'first' ) :
         echo '<td>' . get_sub_field( 'first') . '</td>';
    endif;
    

    To check if not-set, use if ( ! get_sub_field( 'first' ) : (notice the !).

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入