112 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $10,000+ on Envato Market
+3 more
ThemeChills says

Hi everyone,

I’m getting the below error when running my WP theme in DEBUG mode.


Notice: Undefined variable: html in C:\path\...\my-custom-plugin.php on line 129 & 133 

I believe this is the offending code and has something to do with the $post->ID which is being referenced, though have tried multiple tests with no luck.


add_shortcode('chills_flexslider', 'chills_flexslider_shortcode');

function chills_flexslider_shortcode( $atts, $content = null ) {

    extract(shortcode_atts(array(
        'exclude' => '',
        'include' => '',
    ), $atts));

    //comma separated user input IDs to array
    $post_include = isset($include) && $include != '' ? explode( ',', $include) : '' ;
    $post_exclude = isset($exclude) && $exclude != '' ? explode( ',', $exclude) : '' ;

    $loop = new WP_Query(array(
                'post_type' => 'slides',
                'post_status' => 'publish',
                'post__in' => $post_include, //array
                'post__not_in' => $post_exclude, //array
                'ignore_sticky_posts' => 1,
            ));

    if( $loop->have_posts() ) :

        $html .= '<div class="flexslider">
                      <ul class="slides">';

                        while ( $loop->have_posts() ) : $loop->the_post();

                            $the_featured_image = get_the_post_thumbnail( $post->ID, 'full' );
                            $the_caption         = get_the_content();

                            $html.='<li>'.$the_featured_image.'<p class="flex-caption">'.$the_caption.'</p></li>';

                        endwhile; //END loop

            $html .= '</ul>
                    </div>';

    endif;

    return $html;

}

I’d appreciate your thoughts, thanks everyone.

Leo

3445 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Moderator says

You never actually defined/initialized $html before accessing it. Just add

$html = '';

before your if statement and you should be good to go

(the warning message actually tells you exactly what is going on: the variable, $html, which is used in those lines indicated, has not been defined – you just need to define it before you use it)

112 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $10,000+ on Envato Market
+3 more
ThemeChills says

Thanks for the prompt reply @sevenspark.

Funnily enough, I did have that variable declared, though the snippet above didn’t show it. Weird.

Here is the code how it currently stands and is still throwing the same error:


function chills_flexslider_shortcode( $atts, $content = null ) {

    extract(shortcode_atts(array(
        'exclude' => '',
        'include' => '',
    ), $atts));

    //comma separated user input IDs to array
    $post_include = isset($include) && $include != '' ? explode( ',', $include) : '' ;
    $post_exclude = isset($exclude) && $exclude != '' ? explode( ',', $exclude) : '' ;

    $loop = new WP_Query(array(
                'post_type' => 'slides',
                'post_status' => 'publish',
                'post__in' => $post_include, //array
                'post__not_in' => $post_exclude, //array
                'ignore_sticky_posts' => 1,
            ));

    $html = ' '; 

    if( $loop->have_posts() ) :

        $html .= '<div class="flexslider">
                      <ul class="slides">';

                        while ( $loop->have_posts() ) : $loop->the_post();

                            $the_featured_image = get_the_post_thumbnail( $post->ID, 'full' );
                            $the_caption         = get_the_content();

                            $html.='<li>'.$the_featured_image.'<p class="flex-caption">'.$the_caption.'</p></li>';

                        endwhile; //END loop

            $html .= '</ul>
                    </div>';

    endif;

    return $html;

}

Thoughts?

380 posts
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has been part of the Envato Community for over 4 years
  • Sells items exclusively on Envato Market
+2 more
kaaz says

Use get_the_ID() instead of $post->ID in custom loops OR in this case $loop->post->ID

add_shortcode( 'chills_flexslider', 'chills_flexslider_shortcode' );

function chills_flexslider_shortcode( $atts, $content = null ) {

    extract(shortcode_atts(array(
        'exclude' => '',
        'include' => '',
    ), $atts));

    //comma separated user input IDs to array
    $post_include = isset($include) && $include != '' ? explode( ',', $include) : '' ;
    $post_exclude = isset($exclude) && $exclude != '' ? explode( ',', $exclude) : '' ;

    $loop = new WP_Query(array(
                'post_type' => 'slides',
                'post_status' => 'publish',
                'post__in' => $post_include, //array
                'post__not_in' => $post_exclude, //array
                'ignore_sticky_posts' => 1,
            ));

    $html = ''; 

    if( $loop->have_posts() ) :

        $html .= '<div class="flexslider"><ul class="slides">';

                        while ( $loop->have_posts() ) : $loop->the_post();

                            $the_featured_image = get_the_post_thumbnail( get_the_ID(), 'full' );
                            $the_caption = get_the_content();

                            $html .= '<li>'.$the_featured_image.'<p class="flex-caption">'.$the_caption.get_the_title().'</p></li>';

                        endwhile; //END while loop

            $html .= '</ul></div>';

    else:

        $html .= '<p>' . __( '[chills_flexslider] Error: No slides found', 'framework' ) . '</p>';

    endif;

    return $html;

}

Thanks

112 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $10,000+ on Envato Market
+3 more
ThemeChills says

If only there was a rating system for your help @kaaz, you’d get 5 STAR!

Thanks buddy!

380 posts
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has been part of the Envato Community for over 4 years
  • Sells items exclusively on Envato Market
+2 more
kaaz says

you’re welcome :)

by
by
by
by
by
by