0

I receive the following notice Undefined offset: 0 in.........

Category: <?php $category = get_the_category(); 
                    if($category[0]){ 
                    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } 
                ?>
    | <?php comments_popup_link( 'Add a comment', '1 comment', '% Comments' ); ?>
</div>

The error is on this line

if($category[0]){

How to fix the notice.

Elyor
  • 5,396
  • 8
  • 48
  • 76
  • 1
    It's not an error, it's a warning, and it means there is not a $category[0], meaning your $category most likely is empty or does not contain an array.. – Naruto Sep 08 '15 at 11:38
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax Sep 08 '15 at 11:40
  • double check your array – Elyor Sep 08 '15 at 12:03
  • Your array is empty. You have to add categories from dashboard. – Atif Tariq Sep 08 '15 at 12:45

1 Answers1

0

isset check if a Variable is undefined or NULL and return false Function isset() on php.net

if(isset($category) && isset($category[0])) {
    ...
}

or faster and same logic:

if(isset($category, $category[0])) {

}
rob7xiv
  • 51
  • 4