83 posts
  • Attended a Community Meetup
  • Australia
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
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

3422 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark Volunteer 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)

83 posts
  • Attended a Community Meetup
  • Australia
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
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?

369 posts
  • Envato Studio (Microlancer) Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Most Wanted Bounty Winner
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
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

83 posts
  • Attended a Community Meetup
  • Australia
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
ThemeChills says

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

Thanks buddy!

369 posts
  • Envato Studio (Microlancer) Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Most Wanted Bounty Winner
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
kaaz says

you’re welcome :)

by
by
by
by
by
by