0

I'm trying to display the post tags through a widget, I've following codes and getting error "Undefined variable: array":

    if ( $instance['stats_tag']['category'] ) {         
        $posttags = get_the_tags();
        if ($posttags) {
           $array = array();
            foreach($posttags as $tag) {
            $link = get_tag_link($tag->term_id);
              $array[] = '<a href="' . esc_url( $link ) . '" rel="tag" itemprop="keywords">' . $tag->name . '</a>';

        }   
    }   
    if ( $stats != '' ) {
        $stats[] =  '<span class="the-day-tags">' . implode(', ', (array)$array). '</span>' ;
    }

Can anyone help me to fix the issue ?

1 Answer 1

0

You are probably not getting into your first if - And therefore the $array is not defined, since you do it in that condition and also use it if $stats is not empty - Either define the $array before the if($instance) or find another way :-D so something like this, will get you on the right track

$array = array();
if ( $instance['stats_tag']['category'] ) {         
    $posttags = get_the_tags();
    if ($posttags) {

        foreach($posttags as $tag) {
        $link = get_tag_link($tag->term_id);
          $array[] = '<a href="' . esc_url( $link ) . '" rel="tag" itemprop="keywords">' . $tag->name . '</a>';

    }   
} 
if ( $stats != '' ) {
    $stats[] =  '<span class="the-day-tags">' . implode(', ', (array)$array). '</span>' ;
}
2
  • Also, to not confuse yourself later, you should maybe start naming your variables more specific names, like $arrayOfLinks = array();
    – Stender
    Apr 20, 2017 at 10:29
  • Thanks @Stender, now error is fixed, but getting so confusing issue again, post tags are displayed in home page but not in custom page, what's causing it? i'm getting confused.
    – Jivan Rai
    Apr 20, 2017 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.