0

I am using a plugin to manage events on a wordpress website. Events can be taggued, but they do not have any categories, which leads to an issue.

The listing pages lists those events, but since they dont have any categories it shows an error for the events.

The error :

NOTICE: UNDEFINED OFFSET: 0 IN /xxxxxxxx/ARCHIVE.PHP ON LINE 39
NOTICE: TRYING TO GET PROPERTY OF NON-OBJECT IN /xxxxxxxx/ARCHIVE.PHP ON LINE 39

The php code :

<span class="mvp-feat1-cat"><?php $category = get_the_category(); echo esc_html( $category[0]->cat_name ); ?></span>

Any idea how to fix this? I am guessing that I should check if get_the_category() returns an object or an error or something?

Thanks !

Phil
  • 157,677
  • 23
  • 242
  • 245
jeremy
  • 105
  • 10
  • _"I am guessing that I should check if get_the_category() returns an object or an error or something?"_ sounds good, have you tried it out? – Phil Nov 06 '18 at 23:08
  • add a category to it? I would be hesitant in editing 3rd party (plugin) code if you didn't write it as one update could wipe out any "fixes" you do. And then one day 6 or 8 months from now your site will mess up and you will have forgotten the specifics about this and have to debug it all over again. If you do have to edit a plugin, one thing that works nice is to copy it / rename it. And then leave the original one installed but not activated. That way you can see when it has updates, and compare those updates to the code you made changes to. I also put a comment block (With my name) by it – ArtisticPhoenix Nov 06 '18 at 23:16
  • In the code like `/* START:artisticphoenix 2018-11-6 */` and `/* END:artisticphoenix */` that way I can just search through the files for `START:artisticphoenix` and know where my changes are. I won't say I have to fix a lot of plugins, but I do ... lol ... I just can't abide by notices and warnings on most of my sites... :-) – ArtisticPhoenix Nov 06 '18 at 23:22
  • @phil : I did try some "isset" codes yesterday, and obviously emptying the cache etc, but I must have done it wrong as it didnt change anything. – jeremy Nov 07 '18 at 22:42
  • Indeed @ArtisticPhoenix, this is a template file, so there is no risk of losing it with an update of the plugin. Thanks though! – jeremy Nov 07 '18 at 22:42
  • `isset()` won't tell you if an array is empty. For that you want `empty()` or `count()` – Phil Nov 07 '18 at 22:45
  • 1
    @jeremy - sure, I just wanted to mention my method, sometimes there is no choice but to modify a plugin and it's good to have a plan if you do so. Even recently I moved some sites built in 5.3 to PHP7 some plugins I had to update were no longer maintained etc. So it was either edit the plugin or redo the site... lol – ArtisticPhoenix Nov 07 '18 at 22:49

1 Answers1

0

What about checking if $category is set (or if get_the_category() instead returns false/null):

<span class="mvp-feat1-cat"><?php $category = get_the_category(); if (isset($category) && $category) echo esc_html( $category[0]->cat_name ); ?></span>

Edit: I'm guessing archive.php is a theme file, so you should be okay editing it.

cjs1978
  • 477
  • 3
  • 7
  • Thanks @cjs1978 I was indeed looking into isset yesterday. I've tried your code too but doesnt seem to work. I made sure to empty the cache before... and still the same. :-( – jeremy Nov 07 '18 at 22:44
  • I am not sure if that work and it was a cache issue, or another code i added in the functions.php but it seems to have been fixed. It would make sense that the isset works, but it's just that it didnt seem to work the other day Thanks anyway ! – jeremy Nov 12 '18 at 22:02