0

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
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • I recommend doing a manual update for your WordPress instance. https://wordpress.org/support/article/updating-wordpress/#manual-update Let me know if that resolves it. – Brian Nezhad Sep 05 '19 at 18:01
  • @Farhad, this is already a brand new installation. And when I switch back to my old theme, there is no error. This is only an issue within Avada theme. – Sparky Sep 05 '19 at 18:10
  • I figure, but I want you to do it again because the way sometimes core functions get installed can be broken. If that didn't help then we can move into looking at the core file itself. – Brian Nezhad Sep 05 '19 at 18:16
  • @Farhad - I removed and re-uploaded the `wp-admin` and `wp-includes` directories. I re-uploaded all core files except `wp-config.php`. I did not re-upload anything from `wp-content` because nothing in these new files are being used... none of the newer themes, nor Akismet & Hello Dolly. None of this made any difference. Problem persists. – Sparky Sep 05 '19 at 19:08
  • Can you try `get_the_category_list`, https://developer.wordpress.org/reference/functions/get_the_category_list/, and tell me what you get? – Brian Nezhad Sep 05 '19 at 19:12
  • `get_the_category_list()` is doing absolutely nothing. – Sparky Sep 05 '19 at 20:15
  • OK, the way I see it, you `wp-includes/category-template.php` is not being loaded correctly. Try a few other methods from the class and see if you can get any of them working. https://github.com/WordPress/WordPress/blob/master/wp-includes/category-template.php – Brian Nezhad Sep 05 '19 at 20:19
  • @Farhad - `wp_list_categories()` is working fine just above my conditional, so looks like the `category-template.php` file is being loaded ok. – Sparky Sep 05 '19 at 20:34

2 Answers2

0

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.

Sparky
  • 98,165
  • 25
  • 199
  • 285
0

I had this problem with a banner. The hooks in WordPress want you to use isset rather than !. Here is an example of a pagebanner function I corrected to fix this problem:

  
  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 }```
tshirtdr1
  • 39
  • 1
  • 6