WordPress 调用未定义函数 in_category()

WordPress call to undefined function in_category()

我在我的二十三子主题的 wp-content/themes/twentythirteen-child/content.php 模板中有这个条件函数,可以在博客存档页面上的某些帖子上方添加横幅。

if (in_category(get_theme_option('banner_1_category')) 
      && (! is_category(get_theme_option('banner_1_category'))))
{ 
        echo "<div id=\"banner-1\"><h3>".get_theme_option('banner_1_text')."</h3></div>";
} 
....

它运行没有问题。

现在我要转到 Avada 子主题,相应的模板是 wp-content/themes/avada-child/templates/blog-layout.php。当我将上述代码放入此模板中时,出现以下错误:

Fatal error: Call to undefined function in_category()

我不太明白为什么 "undefined" 自 in_category() is a core function of WordPress。我用谷歌搜索了这个错误,但没有找到很多结果。似乎通过在 wp-load.php 文件中添加 require_once() 来解决类似的错误。

require_once('/home/my-server-path/wp-load.php');

我试着把它放在我的条件之前,但没有任何区别。

为什么 WordPress 的一个核心功能在 Avada 中不起作用,我该如何解决?

我是 运行 最新版本的 WordPress (5.2.3) 和 Avada (6.0.3)


编辑:

没有提到上面的条件函数是通过 include

注入到模板中的
include 'my-path/includes/banners.php';

工作:

wp-content/themes/twenty-thirteen-child
    ↳ archive.php ('get_template_part' content-cpt.php) 
    ↳ content-cpt.php ('include' includes/banners.php)
    ↳ includes
        ↳ banners.php

(致命错误:调用未定义函数 in_category()):

wp-content/themes/avada-child
    ↳ archive.php ('get_template_part' templates/blog-layout.php)
    ↳ templates
        ↳ blog-layout.php ('include' includes/banners.php)
    ↳ includes
        ↳ banners.php

我将 include 更改为 get_template_part(),一切都恢复正常了。为了这样做,我不得不将文件名修改为 blog-banners.php

get_template_part('includes/blog', 'banners');

但是,我无法解释为什么 23 中的 include 中的 WordPress 核心功能运行良好,但不同模板中相同 include 中的相同 WordPress 核心功能却提供 "undefined" Avada 中的错误。这两个主题都使用一系列 get_template_part() 函数来结束我插入条件的模板。唯一的区别是插入我的 include 的 Avada 模板位于主题的子目录中,但在二十三中它位于主题的根目录中。

我会接受我自己的答案,直到有人发布更好的答案来详细解释这个致命错误。

我的横幅有这个问题。 WordPress 中的钩子希望您使用 isset 而不是 !。这是我为解决此问题而更正的 pagebanner 函数示例:

  
  if(isset($args['title'])){
    }else{
      $args['title'] = get_the_title();
    }

  if(isset($args['subtitle'])){
    }else{
      $args['subtitle'] = get_field('page_banner_subtitle');
    }

  if(isset($args['photo'])){
    }
    elseif (get_field('page_banner_background_image') AND !is_archive() AND !is_home() ) {
      $args['photo'] = get_field('page_banner_background_image')['sizes']['pageBanner'];
    }
      else{
        $args['photo'] = get_theme_file_uri('/images/ocean.jpg');
      }
  ?>  
  <div class="page-banner">
    <div class="page-banner__bg-image" style="background-image: url(<?php echo $args['photo']; ?>);"></div>
    <div class="page-banner__content container container--narrow">
      <h1 class="page-banner__title"><?php echo $args['title'] ?></h1>
      <div class="page-banner__intro">
        <p><?php echo $args['subtitle']; ?></p>
      </div>
    </div>  
  </div>
<?php }```