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;
1
  • 2
    What would happen if $categories was false? Commented Dec 23, 2013 at 15:58

1 Answer 1

4

$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; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

so simple, silly mistake...Thank you. I will accept when the time limit is up
@AlbaClan In general, it's best to initialize variables before using them. More on this subject in this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.