phpwordpressnotice

WordPress - "Notice: Undefined variable"


It seems like there are some PHP errors in my code where the index variable is not set. How could I fix it?

Fill About.php

<?php
    // Template Name: About
?>

<?php get_header();?>

<?php if(have_posts()): ?>
    <?php while(have_posts()): the_post(); ?>
        <?php
            $args = array(
                'post_parent' => $post->ID,
                'post_type'   => 'page',
                'numberposts' => -1,
                'post_status' => 'publish',
                'orderby' => 'menu_order',
                'order' => 'ASC',
            );
            $subPages = get_children( $args );
            $benefits = get_posts(array(
                'post_type' => 'benefit',
                'numberposts' => 3,
                'orderby' => 'menu_order',
                'order' => 'ASC',
            ));
            $members = get_posts(array(
                'post_type' => 'member',
                'numberposts' => -1,
                'orderby' => 'menu_order',
                'order' => 'ASC',
            ));
        ?>
        <main class="content">
            <section class="about parallax">
                <div data-depth="0.30" class="about-bg-mob-1 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/benefits-img.svg" alt=""/></div>
                <div data-depth="0.60" class="about-bg-mob-2 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-6.svg" alt=""/></div>
                <div data-depth="0.40" class="about-bg-mob-3 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-6.svg" alt=""/></div>
                <div data-depth="0.30" class="about-bg-mob-4 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-3.svg" alt=""/></div>
                <div data-depth="0.70" class="about-bg-mob-5 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-4.svg" alt=""/></div>
                <div data-depth="0.20" class="about-bg-1 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-1.svg" alt=""/></div>
                <div data-depth="0.30" class="about-bg-2 layer"><img src="<?=TDIR?>/assets/dist/s/images/tmp_file/small-img-5.png" alt=""/></div>
                <div data-depth="0.40" class="about-bg-3 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-2.svg" alt=""/></div>
                <div data-depth="0.70" class="about-bg-10 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-6.svg" alt=""/></div>
                <div data-depth="0.70" class="about-bg-11 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-3.svg" alt=""/></div>
                <div class="container">
                    <div data-depth="0.50" class="about-bg-4 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-3.svg" alt=""/></div>
                    <div data-depth="0.60" class="about-bg-5 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-4.svg" alt=""/></div>
                    <div data-depth="0.70" class="about-bg-6 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-6.svg" alt=""/></div>
                    <?php if (!empty($subPages)): ?>
                        <?php foreach ($subPages as $page): ?>
                            <div class="row <?php if ( ($index + 1) == count($subPages)): ?> mb-160 <?php else: ?> mb-37 <?php endif ?>">
                                <?php if ($index == 1): ?>
                                    <div data-depth="0.30" class="about-bg-mob-6 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/customer-bg-3.svg" alt=""/></div>
                                    <div data-depth="0.50" class="about-bg-mob-7 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/h-img.svg" alt=""/></div>
                                    <div data-depth="0.40" class="about-bg-mob-8 layer layer-mob"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-8.svg" alt=""/></div>
                                    <div data-depth="0.60" class="about-bg-7 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/customer-bg-3.svg" alt=""/></div>
                                    <div data-depth="0.70" class="about-bg-8 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/h-img.svg" alt=""/></div>
                                    <div data-depth="0.70" class="about-bg-9 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-8.svg" alt=""/></div>
                                <?php endif ?>
                                <div class="col-md-7 <?php if ($index % 2 == 0): ?> col-md-push-5 col-sm-12 col-sm-push-0 <?php endif ?> ">
                                    <div class="about-img <?php if ($index % 2 != 0): ?> right <?php endif ?>">
                                        <?php if (has_post_thumbnail($page->ID)): ?>
                                            <img <?php if ($index % 2 == 0): ?> style="float: right;" <?php endif ?> src="<?=get_the_post_thumbnail_url($page->ID, 'full')?>" alt=""/>
                                        <?php endif ?>
                                    </div>
                                </div>
                                <div class="col-md-5 <?php if ($index % 2 == 0): ?> col-md-pull-7 col-sm-12 col-sm-pull-0 <?php endif ?>">
                                    <div class="about-txt <?php if ($index % 2 != 0): ?> right <?php endif ?>">
                                        <div class="about-txt__h font-1">
                                            <h3><?=__($page->post_title)?></h3>
                                        </div>
                                        <div class="about-txt__desc font-2">
                                            <p><?=__($page->post_content)?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php $index ++; ?>
                        <?php endforeach ?>
                    <?php endif ?>

                    <div class="row">
                        <?php if (!empty($benefits)): ?>
                            <?php foreach ($benefits as $benefit): ?>
                                <div class="col-sm-4">
                                    <div class="about-item">
                                        <div class="about-item__h font-1">
                                            <h3><?=__($benefit->post_title)?></h3>
                                        </div>
                                        <div class="about-item__desc">
                                            <p><?php echo apply_filters( 'the_content', $benefit->post_content ); ?></p>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach ?>
                        <?php endif ?>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="about__h font-1">
                                <h2><?=__('Our Team','plaza')?></h2>
                            </div>
                        </div>
                    </div>
                    <div class="row special-align">
                        <div data-depth="0.70" class="about-bg-12 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/popular-small-bg-3.svg" alt=""/></div>
                        <div data-depth="0.70" class="about-bg-13 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-6.svg" alt=""/></div>
                        <div data-depth="0.70" class="about-bg-14 layer"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-3.svg" alt=""/></div>
                        <div data-depth="0.70" class="about-bg-15 layer">
                            <div class="img"><img src="<?=TDIR?>/assets/dist/s/images/useful/svg/theme/small-img-2.svg" alt=""/></div>
                        </div>
                        <?php if (!empty($members)): ?>
                            <?php foreach ($members as $member): ?>
                                <div class="col-sm-3">
                                    <div class="about-team">
                                        <div class="about-team__img">
                                            <?php if (has_post_thumbnail($member->ID)): ?>
                                                <img src="<?=get_the_post_thumbnail_url($member->ID, 'large')?>" alt=""/>
                                            <?php endif ?>
                                        </div>
                                        <div class="about-team__h font-1">
                                            <h3><?=__($member->post_title)?></h3>
                                        </div>
                                        <div class="about-team__desc">
                                            <p><?=nl2br(__($member->post_excerpt))?></p>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach ?>
                        <?php endif ?>
                    </div>
                </div>
            </section>

        </main>
    <?php endwhile ?>
<?php endif ?>

<?php get_footer(); ?>

Errors I get Notice: Undefined variable: index in /home/r328061/koopia/wp-content/themes/plaza/about.php on line 51 Notice: Undefined variable: index in /home/r328061/koopia/wp-content/themes/plaza/about.php on line 62 style="float: right;" src="Picture URL" alt=""/>

Notice: Undefined variable: index in /home/r328061/koopia/wp-content/themes/plaza/about.php on line 77


Solution

  • You need to define a value for $index. Add the top of your code:

    <?php
    $index = "1";
    ?>