0

I've got such function like the one below:

function ShowingMenusSep( $atts ) { 
     $atts = shortcode_atts( array(
        'menucats' => '',
    ), $atts );

   if ( empty( $atts['menucats'] ) ) {
        // If category provided, exit early
        return;
    }    

    ob_start(); 

    $args = array (
        'numberposts' => -1,
        'order' => 'ASC',
        'orderby' => 'menu_order',
        'post_type' => array('menus'),
        'menucats' => $atts['menucats'],
    );    


        $query = new WP_Query($args); 
            while( $query->have_posts() ):$query->the_post(); 
            $data .= '<div id="foodmenu-'.get_the_ID().'">';
            $data .= '<div class="one">';
            $data .= '<div class="menu_content_classic">';
            $data .= '<h5 class="menu_post">';
            $data .= '<span class="menu_title">'.get_the_title().'</span>';
            $data .= '<span class="menu_dots"></span>';
            $data .= '<span class="menu_price">'.get_post_meta(get_the_ID(), 'menu_price', TRUE).'&nbsp;'.get_post_meta(get_the_ID(), 'menu_price_currency', TRUE).'</span>';
            $data .= '</h5>';             
            $data .= '<div class="post_detail menu_excerpt">'.get_the_excerpt().'</div>';


            $menu_size_item_title_arr = get_post_meta(get_the_ID(), 'menu_size_item_title', true);
            $menu_size_item_price_arr = get_post_meta(get_the_ID(), 'menu_size_item_price', true);   

            if(!empty($menu_size_item_title_arr) && is_array($menu_size_item_title_arr))
            {
                $data.= '<br class="clear"/>';

                foreach($menu_size_item_title_arr as $key => $menu_size_item_title_item)
                {
                    if(!empty($menu_size_item_title_item))
                    {
                        $data.= '<h5 class="menu_post size"><span class="menu_title size">'.$menu_size_item_title_item.'</span><span class="menu_dots size"></span>';
                    }

                    if(isset($menu_size_item_price_arr[$key]) && !empty($menu_size_item_price_arr[$key]))
                    {
                        $data.= '<span class="menu_price size">'.$menu_size_item_price_arr[$key].'</span>';
                    }
                }
            }

            $data .= '</div>';
            $data .= '</div>';
            $data .= '</div><div class="clear"></div>';   

        endwhile; wp_reset_postdata(); 

        return $data; 
 } 
add_shortcode('FoodLoop','ShowingMenusSep');

When I enable wp_debug i got info, that the line: " $data .= '';"

generates an error - variable data is undefined. Whats wrong with this code? I thought its done properly. Sure, i can disable showing errors but its not the point..

thank you for any suggestions.

Paweł Skaba
  • 671
  • 1
  • 17
  • 49
  • 1
    outside the loop you can declare data `$data=""` you started using data like `$data .= ` but it was not declared anywhere – Masivuye Cokile Nov 15 '18 at 12:20
  • 1
    The issue is what it says. The variable `$data` is undefined (does not exist) when you try and use it the first time (in the first iteration of your loop). – M. Eriksson Nov 15 '18 at 12:20
  • 2
    You cannot use the `.=` concatenator on an undeclared variable. So you need to create an empty string first using `$data = ''; `or your first use of `$data` must be `$data = ` followed by `$data .=` – RiggsFolly Nov 15 '18 at 12:22

0 Answers0