简体   繁体   中英

Call to undefined WordPress function get_header() errors, but header is still displayed

I'm creating a custom WordPress theme, more specifically the index.php file. When I navigate to my website main page, the header is displayed correctly, but occasionally errors show in the server log:

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

I don't know why it gives an error if it displays the header. Also, if I remove <?php get_header(); ?> <?php get_header(); ?> , the header won't get displayed. Can I ignore these errors?

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(); ?>

If you or someone else tries to visit a URL like this: https://yoursite.dev/wp-content/themes/OryTheme

You should get an Uncaught Error: Call to undefined function get_header() in .... error.

Most likely you or someone else is hitting that direct theme directory of your website and it's executing index.php but unfortunately, you won't get any wordpress functions there. So it's logging an error.

If you would like to get rid of this types of errors. You can always put a code like this in the beginning of PHP files of your custom theme and plugins.

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

// start coding here 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM