自定义WordPress主题下文章无法显示的问题排查求助
问题排查与解决方案
核心问题
你直接将静态HTML粘贴到WordPress模板PHP文件中,没有集成WordPress的模板判断与内容渲染逻辑,导致系统无法识别文章请求,始终渲染首页内容。
解决方案
1. 给index.php添加文章判断逻辑
如果你的主题没有单独的文章详情模板,WordPress会默认用index.php处理文章请求。你需要在index.php中加入条件判断,区分首页和文章页:
<?php get_header(); ?> <?php if ( have_posts() ) : ?> <!-- 判断是否为文章详情页 --> <?php if ( is_single() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <h1><?php the_title(); ?></h1> <div class="post-content"> <?php the_content(); ?> </div> <?php endwhile; ?> <?php else : ?> <!-- 这里放你原来的静态首页HTML内容 --> 你的首页HTML代码 <?php endif; ?> <?php endif; ?> <?php get_footer(); ?>
- 若主题没有
header.php和footer.php,直接把HTML的头部、尾部内容整合到index.php对应位置即可。
2. 单独创建文章详情模板(更规范)
新建single.php文件放入主题目录,专门处理文章渲染,WordPress会自动识别并使用该模板:
<?php get_header(); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <article class="single-post"> <h1 class="post-title"><?php the_title(); ?></h1> <div class="post-meta"> 发布时间:<?php the_date(); ?> | 作者:<?php the_author(); ?> </div> <div class="post-content"> <?php the_content(); ?> </div> </article> <?php endwhile; endif; ?> <?php get_footer(); ?>
创建后,访问/?p=78这类链接时,系统会自动用该模板渲染文章内容,不再跳转首页。
3. 额外排查步骤
- 进入后台「设置-固定链接」,无需修改设置,直接点击「保存更改」刷新链接规则;
- 确认当前使用的主题是你修改模板的那一个,模板文件都放在
wp-content/themes/你的主题名/目录下。
内容的提问来源于stack exchange,提问作者Samuel
相关产品推荐
相关产品推荐

