【问题标题】:Advanced Custom Field True/False field not working高级自定义字段真/假字段不起作用
【发布时间】:2017-02-12 17:37:57
【问题描述】:
<?php if( have_rows('team_members') ): ?>
    <?php while( have_rows('team_members') ): the_row(); ?>
        <div class="team-member-section">   
            <div class="container">
                <?php if( get_row_layout() == 'team_members' ): ?>
                    <section class="team-member-section">                                   
                        <?php 
                            $team_title = get_sub_field('team_title');
                            $team_member = get_sub_field('team_member');
                            $description = get_sub_field('description');
                        ?>
                        <div class="team-title">
                            <?php echo $team_title; ?>
                        </div>
                         <?php if( $team_member ): ?>
                            <?php if ( get_field('has_description') == 'yes') : ?>
                                <?php echo 'has_description'; ?>
                            <?php  else : ?>
                                <?php echo 'has_no_description'; ?>
                            <?php endif; ?>  
                        <?php endif; ?>
                    </section>
                <?php endif; ?>
            </div>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

它只显示值“has_no_description”,为什么我的真/假字段不起作用以及如何使它起作用,因此两种情况都会显示。

更新:

<?php if( have_rows('team_members') ): ?>
<?php while( have_rows('team_members') ): the_row(); ?>
    <div class="team-member-section">   
        <div class="container">
            <?php if( get_row_layout() == 'team_members' ): ?>
                <section class="team-member-section">                                   
                    <?php 
                        $team_title = get_sub_field('team_title');
                        $team_member = get_sub_field('team_member');
                        $description = get_sub_field('description');
                    ?>
                    <div class="team-title">
                        <?php echo $team_title; ?>
                    </div>
                     <?php if( $team_member ): ?>
                        <?php if ( get_field('has_description') ) : ?>

                        <?php foreach( $team_member as $post): ?>
                            <?php setup_postdata($post); ?>
                                <div class="col-sm-4">
                                    <div class="team-member">
                                        <div class="member-img">
                                            <?php the_post_thumbnail(); ?>
                                        </div>
                                        <h4><?php the_title(); ?></h4>
                                        <?php the_content(); ?>
                                        <?php echo "Has description"; ?>
                                        <?php $overlay_text = get_field('overlay_text'); ?>

                                        <?php if($overlay_text != ''): ?>
                                            <div class="overlay-text">
                                                <p><?php echo $overlay_text; ?></p>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php wp_reset_postdata(); ?>
                        <?php endforeach; ?>
                        <?php  else : ?>
                        <?php foreach( $team_member as $post): ?>
                            <?php setup_postdata($post); ?>
                                <div class="col-sm-4">
                                    <div class="team-member">
                                        <div class="member-img">
                                            <?php the_post_thumbnail(); ?>
                                        </div>
                                        <h4><?php the_title(); ?></h4>
                                        <?php the_content(); ?>
                                        <?php echo "Doesn not have description"; ?>
                                        <?php $overlay_text = get_field('overlay_text'); ?>

                                        <?php if($overlay_text != ''): ?>
                                            <div class="overlay-text">
                                                <p><?php echo $overlay_text; ?></p>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>


                            <?php wp_reset_postdata(); ?>
                         <?php endforeach; ?>

                        <?php endif; ?>  
                    <?php endif; ?>
                </section>
            <?php endif; ?>
        </div>
    </div>
<?php endwhile; ?>

我发布了我的整个代码。 我认为这里的问题是“has_description”字段仅显示在“团队”自定义帖子类型上,这就是该字段返回 null 的原因,但我不确定。有人可以让它工作吗?

【问题讨论】:

  • ,这里我使用的是字段'has_description'
  • has_description 是一个真/假类型,它显示为一个复选框。
  • 不幸的是,它只显示else分支。
  • 你能显示var_dump( get_field('has_description'));的输出吗?
  • 输出为NULL

标签: php advanced-custom-fields


【解决方案1】:
<?php if( have_rows('team_members') ): ?>
<?php while( have_rows('team_members') ): the_row(); ?>
    <div class="team-member-section">   
        <div class="container">
            <?php if( get_row_layout() == 'team_members' ): ?>
                <section class="team-member-section">                                   
                    <?php 
                        $team_title = get_sub_field('team_title');
                        $team_member = get_sub_field('team_member');
                        $description = get_sub_field('description');
                    ?>
                    <div class="team-title">
                        <?php echo $team_title; ?>
                    </div>
                     <?php if( $team_member ): ?>
                        <?php foreach( $team_member as $post): ?>
                            <?php if ( get_field('has_description') ) : ?>
                            <?php setup_postdata($post); ?>
                                <div class="col-sm-4">
                                    <div class="team-member">
                                        <div class="member-img">
                                            <?php the_post_thumbnail(); ?>
                                        </div>
                                        <h4><?php the_title(); ?></h4>
                                        <?php the_content(); ?>
                                        <?php echo "Has description"; ?>
                                        <?php $overlay_text = get_field('overlay_text'); ?>

                                        <?php if($overlay_text != ''): ?>
                                            <div class="overlay-text">
                                                <p><?php echo $overlay_text; ?></p>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php wp_reset_postdata(); ?>
                            <?php  else : ?>
                                <div class="col-sm-4">
                                    <div class="team-member">
                                        <div class="member-img">
                                            <?php the_post_thumbnail(); ?>
                                        </div>
                                        <h4><?php the_title(); ?></h4>
                                        <?php the_content(); ?>
                                        <?php echo "NOOOOOOO Description"; ?>
                                        <?php $overlay_text = get_field('overlay_text'); ?>

                                        <?php if($overlay_text != ''): ?>
                                            <div class="overlay-text">
                                                <p><?php echo $overlay_text; ?></p>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php endif; ?>  
                        <?php endforeach; ?>
                    <?php endif; ?>
                </section>
            <?php endif; ?>
        </div>
    </div>
<?php endwhile; ?>

它是这样工作的,我将真/假&lt;?php if ( get_field('has_description') ) : ?&gt;字段移到foreach中,因为真/假字段属于团队自定义帖子类型,退出循环将始终使其为NULL

【讨论】:

    猜你喜欢
    • 2014-10-13
    • 2021-07-07
    • 2017-01-08
    • 2017-12-16
    • 2018-08-06
    • 2013-11-25
    • 2016-09-05
    • 2016-08-12
    相关资源
    最近更新 更多