
问题:Warning: Undefined variable $i in /home/wwwroot/…/wp-content/themes/…/category.php on line 60
这个警告提示的是在 category.php 文件的第 60 行中使用了未定义的变量 $i(变量未初始化就被调用)。以下是具体解决方法:
解决步骤:
- 找到目标文件通过文件路径
/home/wwwroot/.../wp-content/themes/.../category.php找到category.php文件(可以通过 FTP 工具或服务器文件管理器访问)。 - 定位第 60 行代码打开
category.php,找到第 60 行,查看代码中$i的使用场景(比如可能是循环计数、序号输出等,例如echo $i;或$i++)。 - 初始化变量
$i在第 60 行 之前 的位置(比如循环开始前、或文件中首次使用$i的位置上方),添加变量初始化代码,例如:php$i = 0; // 或 $i = 1;(根据代码逻辑选择初始值,计数通常从0或1开始)举例:如果第 60 行是循环中的$i++,则需要在循环开始前先定义$i = 0;。
注意事项:
- 操作前建议先备份
category.php文件,避免修改错误导致网站异常。 - 如果不熟悉代码逻辑,可以搜索文件中所有
$i的位置,找到其首次被使用的地方,在前面初始化即可。
完成后,刷新页面,警告会消失。
原始category.php代码:
<?php
/**
* The template for displaying archive pages.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package ...
*/
get_header(); ?>
<div id="primary" class="content-area clear">
<?php if( get_theme_mod('home-layout', 'choice-1') == 'choice-1' ) : ?>
<?php get_template_part('sidebar', '2'); ?>
<?php endif; ?>
<div class="right-col">
<main id="main" class="site-main clear">
<div class="breadcrumbs clear">
<h1>
<?php single_cat_title(''); ?>
</h1>
</div><!-- .breadcrumbs -->
<div id="recent-content" class="content-list">
<?php
if ( have_posts() ) :
$i = 1;
/* Start the Loop */
while ( have_posts() ) : the_post();
get_template_part('template-parts/content', 'list');
$i++;
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div><!-- #recent-content -->
</main><!-- .site-main -->
<?php
if( $i > get_option( 'posts_per_page' ) ) {
get_template_part( 'template-parts/pagination', '' );
} else {
?>
<style type="text/css">
.content-list .hentry.last {
border-bottom: none;
}
</style>
<?php
}
?>
</div><!-- .right-col -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
你提供的代码中,$i 的未定义警告问题出在 当没有文章(have_posts() 为 false)时,$i 没有被初始化,却在后面的分页判断中被调用了。
具体原因:
代码中 $i = 1; 是写在 if ( have_posts() ) : 条件内部的。
- 如果有文章(
have_posts()为 true),$i会被正常初始化; - 如果没有文章(
have_posts()为 false),$i从未被定义,但后面的if( $i > get_option( 'posts_per_page' ) )仍会调用$i,导致警告。
修复方法:
将 $i 的初始化移到 if ( have_posts() ) : 条件 外面,确保无论有没有文章,$i 都被定义。
修改后的代码如下(核心修改处已标注):
<?php
/**
* The template for displaying archive pages.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package ...
*/
get_header(); ?>
<div id="primary" class="content-area clear">
<?php if( get_theme_mod('home-layout', 'choice-1') == 'choice-1' ) : ?>
<?php get_template_part('sidebar', '2'); ?>
<?php endif; ?>
<div class="right-col">
<main id="main" class="site-main clear">
<div class="breadcrumbs clear">
<h1>
<?php single_cat_title(''); ?>
</h1>
</div><!-- .breadcrumbs -->
<div id="recent-content" class="content-list">
<?php
// 关键修改:将 $i 初始化移到条件外面,确保始终定义
$i = 1; // 无论有没有文章,先初始化 $i
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) : the_post();
get_template_part('template-parts/content', 'list');
$i++;
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div><!-- #recent-content -->
</main><!-- .site-main -->
<?php
// 此时 $i 已确保被定义,不会出现警告
if( $i > get_option( 'posts_per_page' ) ) {
get_template_part( 'template-parts/pagination', '' );
} else {
?>
<style type="text/css">
.content-list .hentry.last {
border-bottom: none;
}
</style>
<?php
}
?>
</div><!-- .right-col -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
原理:
通过将 $i = 1; 放在 if ( have_posts() ) : 之前,无论是否有文章,$i 都会被初始化,后续判断 $i 时就不会出现 “未定义” 的警告了。

微信扫一扫打赏
支付宝扫一扫打赏
