0

I am getting the following error in debug mode from my wordpress theme. Likely a very easy fix but I dont see what to do.

UNDEFINED VARIABLE: OUTPUT .... line 34 ($output variable)

$categories = get_the_category();
if($categories) {
    foreach($categories as $category) {
        $output .= '<a href="'.get_category_link( $category->term_id ).'" class="btn-standard-blog" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>';
    }
}
echo $output;
George Brighton
  • 5,131
  • 9
  • 27
  • 36
memyselfandmyiphone
  • 1,080
  • 4
  • 21
  • 43

1 Answers1

3

$output only exists if the the condition of your IF statement is met. Otherwise you are trying to use a variable that is not yet defined. This is especially true in your case as the first iteration of your loop attempts to add a value to a non-existant value as well so this error will always occur in this code.

You can solve this by declaring this variable with no value and then modifying it when appropriate.

<?php 
$output = '';
$categories = get_the_category();
    if($categories) {
        foreach($categories as $category) {
            $output .= '<a href="'.get_category_link( $category->term_id ).'" class="btn-standard-blog" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>';
    }
}
echo $output; ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • so simple, silly mistake...Thank you. I will accept when the time limit is up – memyselfandmyiphone Dec 23 '13 at 15:57
  • @AlbaClan In general, it's best to initialize variables before using them. More on this subject in [this question](http://stackoverflow.com/questions/17696289/pre-declare-all-private-local-variables). – Boaz Dec 23 '13 at 16:07