如何解决Wordpress 通知:未定义的变量
有人可以帮我解决这个问题。
我的代码中似乎存在一些 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,));
$members = get_posts(array(
'post_type' => 'member',));
?>
<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(); ?>
我得到的错误 注意:未定义变量:index in /home/r328061/koopia/wp-content/themes/plaza/about.PHP 第 51 行 注意:未定义变量:index in /home/r328061/koopia/wp-content/themes/plaza/about.PHP on line 62 风格=“浮动:右;” src="图片网址" alt=""/>
注意:未定义变量:第 77 行 /home/r328061/koopia/wp-content/themes/plaza/about.PHP 中的索引
解决方法
您需要为 $index 定义一个值。添加代码的顶部:
<?php
$index = "1";
?>
,
只需在 php 文件顶部定义 $index
变量:
<?php $index = 0; ?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。