未捕获的错误:调用未定义的函数 have_post()

Posted

技术标签:

【中文标题】未捕获的错误:调用未定义的函数 have_post()【英文标题】:Uncaught Error: Call to undefined function have_post() 【发布时间】:2017-07-25 19:58:24 【问题描述】:

我正在尝试查看我的 WordPress 主题并收到此错误:

致命错误:未捕获的错误:调用 C:\xampp\htdocs\wordpress\wp-content\themes\ChachoTheme\index.php:6 中的未定义函数 have_post() 堆栈跟踪:#0 C:\xampp\htdocs \wordpress\wp-includes\template-loader.php(74): include() #1 C:\xampp\htdocs\wordpress\wp-blog-header.php(19): require_once('C:\xampp\htdocs ...') #2 C:\xampp\htdocs\wordpress\index.php(17): require('C:\xampp\htdocs...') #3 main 在 C:\xampp\htdocs 中抛出\wordpress\wp-content\themes\ChachoTheme\index.php 在第 6 行

index.php:

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

<div id="post">

  <h2><a href="<?php the_permalink(); ?>"></a><?php the_title(); ?></h2>
  <div class="byline">Escrito por <?php the_autor_posts_link(); ?>
    el <a href="<?php the_permalink(); ?>"><?php the_time('l F d, Y'); ?></a>
  </div>
  <?php the_content('Read More..'); ?>
<?php endwhile; ?>
<?php else: ?>
  <p>No posts were found. Sorry!")</p>
<?php endif; ?>

我该如何解决?

【问题讨论】:

问题不在于 index.php 本身,而是对 the_content 的调用在内部调用了一个不存在的函数 have_post()。 (或者可能没有包含在上下文中。) 【参考方案1】:

试试have_posts(),而不是你的have_post()

还将the_autor_posts_link() 更改为the_author_posts_link()

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

<div id="post">

  <h2><a href="<?php the_permalink(); ?>"></a><?php the_title(); ?></h2>
  <div class="byline">Escrito por <?php the_author_posts_link(); ?>
    el <a href="<?php the_permalink(); ?>"><?php the_time('l F d, Y'); ?></a>
  </div>
  <?php the_content('Read More..'); ?>
<?php endwhile; ?>
<?php else: ?>
  <p>No posts were found. Sorry!")</p>
<?php endif; ?>

【讨论】:

【参考方案2】:

这样写你的代码:

<?php
    if ( have_posts() ) 
        while ( have_posts() ) 
?>
            <div id="post">
                <h2><a href="<?php the_permalink(); ?>"></a><?php the_title(); ?></h2>
                <div class="byline">Escrito por <?php the_autor_posts_link(); ?> el <a href="<?php the_permalink(); ?>"><?php the_time('l F d, Y'); ?></a></div>
                <?php the_content('Read More..'); ?>
            </div>

<?php
        
    
    else  ?>
        <p>No posts were found. Sorry!")</p>

<?php
    
?>

【讨论】:

以上是关于未捕获的错误:调用未定义的函数 have_post()的主要内容,如果未能解决你的问题,请参考以下文章

未捕获的错误:调用未定义的函数 Composer\Console\json_decode()

致命错误:未捕获错误:调用未定义函数 is_product()

致命错误:未捕获的错误:调用 C:\xampp\htdocs\ 中未定义的函数 mysql_connect()

未捕获的错误:将 laravel 5.8 升级到 8 后调用未定义的函数 Illuminate\Mail\TransportManager()

致命错误:未捕获的错误:调用 /homepages/ 中未定义的函数 mysql_pconnect() [重复]

致命错误:未捕获的错误:调用未定义的函数 mssql_query() [重复]