Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Catch ajax requests
- add_action( 'wp_ajax_kleo_ajax_search', 'kleo_ajax_search' );
- add_action( 'wp_ajax_nopriv_kleo_ajax_search', 'kleo_ajax_search' );
- if ( ! function_exists( 'kleo_ajax_search' ) ) {
- function kleo_ajax_search() {
- //if "s" input is missing exit
- if ( empty( $_REQUEST['s'] ) ) {
- die();
- }
- $output = "";
- $defaults = array(
- 'numberposts' => 4,
- 'post_type' => 'any',
- 'post_status' => 'publish',
- 'post_password' => '',
- 'suppress_filters' => false
- );
- $defaults = apply_filters( 'kleo_ajax_query_args', $defaults );
- $query = array_merge( $defaults, $_REQUEST );
- $query = http_build_query( $query );
- $posts = get_posts( $query );
- //if there are no posts
- if ( empty( $posts ) ) {
- $output = "<div class='kleo_ajax_entry ajax_not_found'>";
- $output .= "<div class='ajax_search_content'>";
- $output .= "<i class='icon icon-exclamation-sign'></i> ";
- $output .= __( "Sorry, no pages matched your criteria.", 'kleo_framework' );
- $output .= "<br>";
- $output .= __( "Please try searching by different terms.", 'kleo_framework' );
- $output .= "</div>";
- $output .= "</div>";
- echo $output;
- die();
- }
- //if there are posts
- $post_types = array();
- $post_type_obj = array();
- foreach ( $posts as $post ) {
- $post_types[ $post->post_type ][] = $post;
- if ( empty( $post_type_obj[ $post->post_type ] ) ) {
- $post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type );
- }
- }
- foreach ( $post_types as $ptype => $post_type ) {
- if ( isset( $post_type_obj[ $ptype ]->labels->name ) ) {
- $output .= "<h4>" . $post_type_obj[ $ptype ]->labels->name . "</h4>";
- } else {
- $output .= "<hr>";
- }
- foreach ( $post_type as $post ) {
- $format = get_post_format( $post->ID );
- if ( get_the_post_thumbnail( $post->ID, 'thumbnail' ) ) {
- $image = get_the_post_thumbnail( $post->ID, 'thumbnail' );
- } else {
- if ( $format == 'video' ) {
- $image = "<i class='icon icon-film'></i>";
- } elseif ( $format == 'image' || $format == 'gallery' ) {
- $image = "<i class='icon icon-picture'></i>";
- } else {
- $image = "<i class='icon-info-sign'></i>";
- }
- }
- $excerpt = "";
- if ( ! empty( $post->post_content ) ) {
- $excerpt = "<br>" . char_trim( trim( strip_tags( strip_shortcodes( $post->post_content ) ) ), 40, "..." );
- }
- $link = apply_filters( 'kleo_custom_url', get_permalink( $post->ID ) );
- $classes = "format-" . $format;
- $output .= "<div class ='kleo_ajax_entry $classes'>";
- $output .= "<div class='ajax_search_image'>$image</div>";
- $output .= "<div class='ajax_search_content'>";
- $output .= "<a href='$link' class='search_title'>";
- $output .= get_the_title( $post->ID );
- $output .= "</a>";
- $output .= "<span class='search_excerpt'>";
- $output .= $excerpt;
- $output .= "</span>";
- $output .= "</div>";
- $output .= "</div>";
- }
- }
- $output .= "<a class='ajax_view_all' href='" . home_url( '?s=' . $_REQUEST['s'] ) . "'>" . __( 'View all results', 'kleo_framework' ) . "</a>";
- echo $output;
- die();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement