jmccole83
9/12/2018 - 6:30 PM

MaterializeCSS | Auto-generate autocomplete

Use the below snippet to generate the list of values for a seach autocomplete in MaterializeCSS. This example uses Laravel instead of PHP.

jQuery(document).ready(function(){
    
    @php 
    
      $args = array(
        'post_type' => array(
          'post',
          'page',
          'projects',
          'product'
        )
      );
    
    $loop = new WP_Query($args);
    
    @endphp
    
    jQuery('input.autocomplete').autocomplete({
      data: {
        @php
          while ( $loop->have_posts() ) : $loop->the_post(); 
        @endphp
            "@php the_title(); @endphp": null,
        @php 
          endwhile; wp_reset_query();
        @endphp
      },
    });
    
  });