Search code examples
phpwordpresswordpress-themingundefined-function

WordPress call to undefined function in_category()


I have this conditional function inside the wp-content/themes/twentythirteen-child/content.php template of my Twenty Thirteen child theme to add banners above certain posts on the blog archive page.

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>";
} 
....

And it's working without problems.

Now I'm moving to an Avada child theme and the corresponding template is wp-content/themes/avada-child/templates/blog-layout.php. When I put the above code within this template, I get the following error:

Fatal error: Call to undefined function in_category()

I don't quite understand how this can be "undefined" since in_category() is a core function of WordPress. I've Googled the error without many results. It seems that similar errors have been solved by adding a require_once() to the wp-load.php file.

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

I tried putting that ahead of my conditional and it made no difference.

Why is a core function of WordPress not working in Avada and what can I do to fix it?

I'm running the latest versions of WordPress (5.2.3) and Avada (6.0.3)


EDIT:

Failed to mention that the conditional function above is being injected into the templates via an include

include 'my-path/includes/banners.php';

Working:

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

(Fatal error: Call to undefined function 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

Solution

  • I changed the include to a get_template_part() and it's all working again. I had to modify the file name with a slug to blog-banners.php in order to do it this way.

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

    However, I cannot explain why the WordPress core functions within the include in Twenty Thirteen are working fine, but the same WordPress core functions within the same include within a different template are giving "undefined" errors in Avada. Both themes use a series of get_template_part() functions to end up on the template where I inserted my conditional. The only difference is that the Avada template where my include was inserted is within a subdirectory of the theme, but in Twenty Thirteen it's in the root of the theme.

    I'll accept my own answer until somebody posts a better one that explains this fatal error in detail.