预期来自使用ACF插件的php循环的语句错误

时间:2017-11-27 作者:David A. French

我觉得我已经多次检查我的php循环结构,但我不知道我做错了什么。该文件被PHPstorm视为无效,无法正常运行。代码如下:

while ( have_posts() ) : the_post();
$initialCardLoad = 5;
$loopLazyLoad = 0;
$galleryType = get_field(\'gallery_type\'); ?>


    <section class="grid-content">
        <div class="gridGallery <?php the_title_attribute(); ?>">

            <?php


            // check if the repeater field has rows of data
            if (have_rows(\'card\')) {

            // loop through the rows of data
            while (have_rows(\'card\')) : the_row();
            $loopLazyLoad++; // iterate on loop each time you loop through
            // display a sub field value inside a card

            //VARIABLES//
            $title = get_sub_field(\'card_title\');
            $childImage = get_sub_field(\'card_picture\');
            $file = get_sub_field(\'card_video\');
            $video = $file[\'url\'];
            $videoTitle = get_sub_field(\'card_video_title\');
            $cardLink = get_sub_field(\'card_link\');
            $cardEmbed = get_sub_field(\'card_embed\');

            if ($loopLazyLoad > $initialCardLoad) {

            if ($cardEmbed) { ?>
            <div class="grid-item">
                <a data-fancybox data-type="iframe" data-src="http://codepen.io/fancyapps/full/jyEGGG/"
                   href="javascript:;">
                    <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                </a>
                <!-- end of iframe If statement --> <?php
                }

                elseif ($childImage) { ?>
                <div class="grid-item">
                    <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                       href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here ?>">
                        <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                    </a>
                    <!-- End of image if statement --> <?php
                    }



                    elseif ($video) { ?>
                    <div class="grid-item">

                        <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                            <video class="gallery-video lazy" loop autoplay muted>
                                <source src="<?php echo $video; ?>" type="video/mp4">
                            </video>
                        </a>

                        <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                               id="<?php echo $videoTitle ?>">
                            <source src="<?php echo $video; ?>" type="video/mp4">
                        </video>
                        <!-- end of Video If statement -->
                        <?php
                        } ?>
                    </div> <?php //end of grid item div

                    } // End of if lazy load iter check

                    else { // this is where initial load begins

                    if ($cardEmbed) { ?>
                    <div class="grid-item">
                        <a data-fancybox data-type="iframe"
                           data-src="http://codepen.io/fancyapps/full/jyEGGG/" href="javascript:;">
                            <img class="" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                        </a>
                        <!-- end of iframe If statement --> <?php
                        }



                        elseif ($childImage){ ?>
                        <div class="grid-item">
                            <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                               href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here
                               ?>">
                                <img class="" src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                            </a>
                            <!--end of Image IF statement --> <?php
                            }


                            elseif ($video){ ?>
                            <div class="grid-item">

                                <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                                    <video class="gallery-video" loop autoplay muted>
                                        <source src="<?php echo $video; ?>" type="video/mp4">
                                    </video>
                                </a>

                                <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                                       id="<?php echo $videoTitle ?>">
                                    <source src="<?php echo $video; ?>" type="video/mp4">
                                </video>
                                <!-- end of Video If statement --> <?php
                                } ?>

                            </div> <!-- end of grid item div -->

                            <!--end of initial card load of loop-->


                            <?php } //end of else for the initial load
///////////// The above curly bracket is where the problem appears ^^^^^^

                            else {
                                ?> <p>no rows found</p>


                            <?php } //end of else
                            endwhile; ////end of while statement that loops for as long as rows have data
                            } //end of top level if statement that checks if rows have data ?>


                </div>

    </section>

    <script>
        domReady(function () {
            var macy_instance = Macy.init({
                container: \'.<?php the_title_attribute(); ?>\',
                trueOrder: false,
                waitForImages: true,
                debug: false,
                margin: 0,
                columns: 5,
                breakAt: {
                    1200: 4,
                    940: 3,
                    520: 1
                }
            });


            macy_instance.runOnImageLoad(function () {
                macy_instance.recalculate(true);
            }, true);

        });

    </script>

<?php endwhile;?>

1 个回复
最合适的回答,由SO网友:Greg36 整理而成

之后The above curly bracket is where the problem appears 你有第二个else 声明,第一个else 没有if 语句已接近this is where initial load begins 你错过了逻辑。

我清理了你的代码并更正了错误,以后注意不要重复你的代码。在每个语句中,您都有一个具有相同类名的开头div,我将它们移到了条件之前。

<?php

while ( have_posts() ) : the_post();
$initialCardLoad = 5;
$loopLazyLoad    = 0;
$galleryType     = get_field( \'gallery_type\' ); ?>

<section class="grid-content">
    <div class="gridGallery <?php the_title_attribute(); ?>">

        <?php
        // check if the repeater field has rows of data
        if ( have_rows( \'card\' ) ) {

            // loop through the rows of data
            while ( have_rows( \'card\' ) ) :

                the_row();
                $loopLazyLoad ++; // iterate on loop each time you loop through
                // display a sub field value inside a card

                //VARIABLES//
                $title      = get_sub_field( \'card_title\' );
                $childImage = get_sub_field( \'card_picture\' );
                $file       = get_sub_field( \'card_video\' );
                $video      = $file[\'url\'];
                $videoTitle = get_sub_field( \'card_video_title\' );
                $cardLink   = get_sub_field( \'card_link\' );
                $cardEmbed  = get_sub_field( \'card_embed\' );

                if ( $loopLazyLoad > $initialCardLoad ) {

                    ?> <div class="grid-item"> <?php

                    if ( $cardEmbed ) { ?>
                            <a data-fancybox data-type="iframe" data-src="http://codepen.io/fancyapps/full/jyEGGG/"
                               href="javascript:;">
                                <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                            </a>
                            <!-- end of iframe If statement --> <?php

                    }  else if ( $childImage ) { ?>
                            <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                               href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here ?>">
                                <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                            </a>
                        <!-- End of image if statement --> <?php

                    } else if ( $video ) { ?>
                            <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                                <video class="gallery-video lazy" loop autoplay muted>
                                    <source src="<?php echo $video; ?>" type="video/mp4">
                                </video>
                            </a>

                            <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                                   id="<?php echo $videoTitle ?>">
                                <source src="<?php echo $video; ?>" type="video/mp4">
                            </video>
                            <!-- end of Video If statement --> <?php
                    } ?>

                    </div> <?php //end of grid item div

                    // End of if lazy load iter check
                    // this is where initial load begins
                    // HERE PUT YOUR LOGIC
                } else if (true) { 

                    ?> <div class="grid-item"> <?php

                    if ( $cardEmbed ) { ?>
                        <a data-fancybox data-type="iframe"
                           data-src="http://codepen.io/fancyapps/full/jyEGGG/" href="javascript:;">
                            <img class="" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                        </a>
                        <!-- end of iframe If statement --> <?php

                    } else if ( $childImage ){ ?>
                        <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                           href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here
                           ?>">
                            <img class="" src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                        </a>
                        <!--end of Image IF statement --> <?php

                    } else if ( $video ){ ?>
                        <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                            <video class="gallery-video" loop autoplay muted>
                                <source src="<?php echo $video; ?>" type="video/mp4">
                            </video>
                        </a>

                        <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                               id="<?php echo $videoTitle ?>">
                            <source src="<?php echo $video; ?>" type="video/mp4">
                        </video>
                            <!-- end of Video If statement --> <?php
                    } ?>

                    </div> <!-- end of grid item div -->
                    <!--end of initial card load of loop-->  <?php

                    //end of else for the initial load
                } else {
                    ?> <p>no rows found</p> <?php

                } //end of else

            endwhile; ////end of while statement that loops for as long as rows have data
                /// 
        } //end of top level if statement that checks if rows have data ?>


    </div>

</section>

<script>
    domReady( function () {
        var macy_instance = Macy.init( {
            container: \'.<?php the_title_attribute(); ?>\',
            trueOrder: false,
            waitForImages: true,
            debug: false,
            margin: 0,
            columns: 5,
            breakAt: {
                1200: 4,
                940: 3,
                520: 1
            }
        } );


        macy_instance.runOnImageLoad( function () {
            macy_instance.recalculate( true );
        }, true );

    } );

</script>

<?php endwhile; ?>

结束

相关推荐

Show the excerpt in a loop

我想用这个循环显示最后3篇文章。这很有效,但我不知道为什么,但摘录总是一样的。我做错了什么? <?php $args = array( \'numberposts\' => \'3\' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ ?> <div class=\"

预期来自使用ACF插件的php循环的语句错误 - 小码农CODE - 行之有效找到问题解决它

预期来自使用ACF插件的php循环的语句错误

时间:2017-11-27 作者:David A. French

我觉得我已经多次检查我的php循环结构,但我不知道我做错了什么。该文件被PHPstorm视为无效,无法正常运行。代码如下:

while ( have_posts() ) : the_post();
$initialCardLoad = 5;
$loopLazyLoad = 0;
$galleryType = get_field(\'gallery_type\'); ?>


    <section class="grid-content">
        <div class="gridGallery <?php the_title_attribute(); ?>">

            <?php


            // check if the repeater field has rows of data
            if (have_rows(\'card\')) {

            // loop through the rows of data
            while (have_rows(\'card\')) : the_row();
            $loopLazyLoad++; // iterate on loop each time you loop through
            // display a sub field value inside a card

            //VARIABLES//
            $title = get_sub_field(\'card_title\');
            $childImage = get_sub_field(\'card_picture\');
            $file = get_sub_field(\'card_video\');
            $video = $file[\'url\'];
            $videoTitle = get_sub_field(\'card_video_title\');
            $cardLink = get_sub_field(\'card_link\');
            $cardEmbed = get_sub_field(\'card_embed\');

            if ($loopLazyLoad > $initialCardLoad) {

            if ($cardEmbed) { ?>
            <div class="grid-item">
                <a data-fancybox data-type="iframe" data-src="http://codepen.io/fancyapps/full/jyEGGG/"
                   href="javascript:;">
                    <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                </a>
                <!-- end of iframe If statement --> <?php
                }

                elseif ($childImage) { ?>
                <div class="grid-item">
                    <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                       href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here ?>">
                        <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                    </a>
                    <!-- End of image if statement --> <?php
                    }



                    elseif ($video) { ?>
                    <div class="grid-item">

                        <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                            <video class="gallery-video lazy" loop autoplay muted>
                                <source src="<?php echo $video; ?>" type="video/mp4">
                            </video>
                        </a>

                        <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                               id="<?php echo $videoTitle ?>">
                            <source src="<?php echo $video; ?>" type="video/mp4">
                        </video>
                        <!-- end of Video If statement -->
                        <?php
                        } ?>
                    </div> <?php //end of grid item div

                    } // End of if lazy load iter check

                    else { // this is where initial load begins

                    if ($cardEmbed) { ?>
                    <div class="grid-item">
                        <a data-fancybox data-type="iframe"
                           data-src="http://codepen.io/fancyapps/full/jyEGGG/" href="javascript:;">
                            <img class="" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                        </a>
                        <!-- end of iframe If statement --> <?php
                        }



                        elseif ($childImage){ ?>
                        <div class="grid-item">
                            <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                               href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here
                               ?>">
                                <img class="" src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                            </a>
                            <!--end of Image IF statement --> <?php
                            }


                            elseif ($video){ ?>
                            <div class="grid-item">

                                <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                                    <video class="gallery-video" loop autoplay muted>
                                        <source src="<?php echo $video; ?>" type="video/mp4">
                                    </video>
                                </a>

                                <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                                       id="<?php echo $videoTitle ?>">
                                    <source src="<?php echo $video; ?>" type="video/mp4">
                                </video>
                                <!-- end of Video If statement --> <?php
                                } ?>

                            </div> <!-- end of grid item div -->

                            <!--end of initial card load of loop-->


                            <?php } //end of else for the initial load
///////////// The above curly bracket is where the problem appears ^^^^^^

                            else {
                                ?> <p>no rows found</p>


                            <?php } //end of else
                            endwhile; ////end of while statement that loops for as long as rows have data
                            } //end of top level if statement that checks if rows have data ?>


                </div>

    </section>

    <script>
        domReady(function () {
            var macy_instance = Macy.init({
                container: \'.<?php the_title_attribute(); ?>\',
                trueOrder: false,
                waitForImages: true,
                debug: false,
                margin: 0,
                columns: 5,
                breakAt: {
                    1200: 4,
                    940: 3,
                    520: 1
                }
            });


            macy_instance.runOnImageLoad(function () {
                macy_instance.recalculate(true);
            }, true);

        });

    </script>

<?php endwhile;?>

1 个回复
最合适的回答,由SO网友:Greg36 整理而成

之后The above curly bracket is where the problem appears 你有第二个else 声明,第一个else 没有if 语句已接近this is where initial load begins 你错过了逻辑。

我清理了你的代码并更正了错误,以后注意不要重复你的代码。在每个语句中,您都有一个具有相同类名的开头div,我将它们移到了条件之前。

<?php

while ( have_posts() ) : the_post();
$initialCardLoad = 5;
$loopLazyLoad    = 0;
$galleryType     = get_field( \'gallery_type\' ); ?>

<section class="grid-content">
    <div class="gridGallery <?php the_title_attribute(); ?>">

        <?php
        // check if the repeater field has rows of data
        if ( have_rows( \'card\' ) ) {

            // loop through the rows of data
            while ( have_rows( \'card\' ) ) :

                the_row();
                $loopLazyLoad ++; // iterate on loop each time you loop through
                // display a sub field value inside a card

                //VARIABLES//
                $title      = get_sub_field( \'card_title\' );
                $childImage = get_sub_field( \'card_picture\' );
                $file       = get_sub_field( \'card_video\' );
                $video      = $file[\'url\'];
                $videoTitle = get_sub_field( \'card_video_title\' );
                $cardLink   = get_sub_field( \'card_link\' );
                $cardEmbed  = get_sub_field( \'card_embed\' );

                if ( $loopLazyLoad > $initialCardLoad ) {

                    ?> <div class="grid-item"> <?php

                    if ( $cardEmbed ) { ?>
                            <a data-fancybox data-type="iframe" data-src="http://codepen.io/fancyapps/full/jyEGGG/"
                               href="javascript:;">
                                <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                            </a>
                            <!-- end of iframe If statement --> <?php

                    }  else if ( $childImage ) { ?>
                            <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                               href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here ?>">
                                <img class="lazy" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                            </a>
                        <!-- End of image if statement --> <?php

                    } else if ( $video ) { ?>
                            <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                                <video class="gallery-video lazy" loop autoplay muted>
                                    <source src="<?php echo $video; ?>" type="video/mp4">
                                </video>
                            </a>

                            <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                                   id="<?php echo $videoTitle ?>">
                                <source src="<?php echo $video; ?>" type="video/mp4">
                            </video>
                            <!-- end of Video If statement --> <?php
                    } ?>

                    </div> <?php //end of grid item div

                    // End of if lazy load iter check
                    // this is where initial load begins
                    // HERE PUT YOUR LOGIC
                } else if (true) { 

                    ?> <div class="grid-item"> <?php

                    if ( $cardEmbed ) { ?>
                        <a data-fancybox data-type="iframe"
                           data-src="http://codepen.io/fancyapps/full/jyEGGG/" href="javascript:;">
                            <img class="" data-src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                        </a>
                        <!-- end of iframe If statement --> <?php

                    } else if ( $childImage ){ ?>
                        <a data-fancybox="gallery" data-caption="<?php echo $title ?>"
                           href="<?php echo $childImage[\'sizes\'][\'large\'];//big one here
                           ?>">
                            <img class="" src="<?php echo $childImage[\'sizes\'][\'medium\']; ?>">
                        </a>
                        <!--end of Image IF statement --> <?php

                    } else if ( $video ){ ?>
                        <a data-fancybox data-src="#<?php echo $videoTitle ?>" href="javascript:;">
                            <video class="gallery-video" loop autoplay muted>
                                <source src="<?php echo $video; ?>" type="video/mp4">
                            </video>
                        </a>

                        <video style="display: none;" class="gallery-video lazy" loop autoplay muted
                               id="<?php echo $videoTitle ?>">
                            <source src="<?php echo $video; ?>" type="video/mp4">
                        </video>
                            <!-- end of Video If statement --> <?php
                    } ?>

                    </div> <!-- end of grid item div -->
                    <!--end of initial card load of loop-->  <?php

                    //end of else for the initial load
                } else {
                    ?> <p>no rows found</p> <?php

                } //end of else

            endwhile; ////end of while statement that loops for as long as rows have data
                /// 
        } //end of top level if statement that checks if rows have data ?>


    </div>

</section>

<script>
    domReady( function () {
        var macy_instance = Macy.init( {
            container: \'.<?php the_title_attribute(); ?>\',
            trueOrder: false,
            waitForImages: true,
            debug: false,
            margin: 0,
            columns: 5,
            breakAt: {
                1200: 4,
                940: 3,
                520: 1
            }
        } );


        macy_instance.runOnImageLoad( function () {
            macy_instance.recalculate( true );
        }, true );

    } );

</script>

<?php endwhile; ?>

相关推荐

Increase offset while looping

我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:-第一个查询显示从1到4的帖子-第二个查询显示从5到8的帖子-第三个查询显示从9到12的帖子等。 <div class=\"official-matters-tabs\"> <?php $args = array(\'post_type\' => \'official-matters\', \'showp