调用未定义的WordPress函数get_header()错误,但仍显示标题

问题描述 投票:0回答:1

我正在创建一个自定义的WordPress主题,更具体地说是index.php文件。当我导航到我的网站主页时,标题会正确显示,但有时会在服务器日志中显示错误:

PHP Fatal error:  Uncaught Error: Call to undefined function get_header() in D:\home\site\wwwroot\wp-content\themes\OryTheme\index.php:1
Stack trace:
#0 {main}
  thrown in D:\home\site\wwwroot\wp-content\themes\OryTheme\index.php on line 1

我不知道为什么它显示标题时会出错。此外,如果我删除<?php get_header(); ?>,标题将不会显示。我可以忽略这些错误吗?

的index.php

<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div class="col-lg-6 col-sm-6 col-md-6 col-xs-12">
    <h1><?php the_title(); ?></h1>
    <h4>Posted on <?php the_time('F jS, Y') ?></h4>
    <p><?php the_content(__('(more...)')); ?></p>
    </div>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>
<?php get_sidebar(); ?>

<?php get_footer(); ?>
php html wordpress wordpress-theming
1个回答
0
投票

如果您或其他人试图访问这样的网址:https://yoursite.dev/wp-content/themes/OryTheme

你应该得到一个Uncaught Error: Call to undefined function get_header() in ....错误。

很可能你或其他人正在点击你网站的直接主题目录并且它正在执行index.php,但不幸的是,你不会在那里获得任何wordpress功能。所以它记录了一个错误。

如果你想摆脱这种类型的错误。您总是可以在自定义主题和插件的PHP文件的开头放置这样的代码。

<?php 
if ( !defined( 'ABSPATH' ) ) exit; // don't allow to call the file directly

// start coding here 
© www.soinside.com 2019 - 2024. All rights reserved.