【问题标题】:Call to undefined WordPress function get_header() errors, but header is still displayed调用未定义的 WordPress 函数 get_header() 错误,但仍显示标题
【发布时间】:2017-12-19 16:22:34
【问题描述】:

我正在创建一个自定义 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】:

    如果您或其他人试图访问这样的 URL: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 
    

    【讨论】:

    • 感谢您的回答。我真的不明白你点击直接主题目录是什么意思。我在 Azure 中托管服务器,有时当我导航到我的主页(从而加载我的主题的 index.php)时,Azure 服务器控制台会显示错误。不过,正如我所说,标题加载得非常好,如果没有那行,主题将无法工作。
    • “命中”这个词的意思是访问那个 URL。只需尝试在index.php 中使用这些代码,您就永远不会在日志中出现该错误。 pastebin.com/jhuJbHz3
    • 仅供参考,if ( !defined( 'ABSPATH' ) ) exit; 这行代码只是为了检查 WordPress 是否正在执行/包含该文件。如果这个文件是从某个地方手动调用的,它只会退出而不是抛出错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2014-11-13
    • 2013-11-15
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多