Advertisement
rdusnr

Untitled

Jul 26th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1.  
  2. //Catch ajax requests
  3. add_action( 'wp_ajax_kleo_ajax_search', 'kleo_ajax_search' );
  4. add_action( 'wp_ajax_nopriv_kleo_ajax_search', 'kleo_ajax_search' );
  5. if ( ! function_exists( 'kleo_ajax_search' ) ) {
  6. function kleo_ajax_search() {
  7. //if "s" input is missing exit
  8. if ( empty( $_REQUEST['s'] ) ) {
  9. die();
  10. }
  11.  
  12. $output = "";
  13. $defaults = array(
  14. 'numberposts' => 4,
  15. 'post_type' => 'any',
  16. 'post_status' => 'publish',
  17. 'post_password' => '',
  18. 'suppress_filters' => false
  19. );
  20. $defaults = apply_filters( 'kleo_ajax_query_args', $defaults );
  21.  
  22. $query = array_merge( $defaults, $_REQUEST );
  23. $query = http_build_query( $query );
  24. $posts = get_posts( $query );
  25.  
  26. //if there are no posts
  27. if ( empty( $posts ) ) {
  28. $output = "<div class='kleo_ajax_entry ajax_not_found'>";
  29. $output .= "<div class='ajax_search_content'>";
  30. $output .= "<i class='icon icon-exclamation-sign'></i> ";
  31. $output .= __( "Sorry, no pages matched your criteria.", 'kleo_framework' );
  32. $output .= "<br>";
  33. $output .= __( "Please try searching by different terms.", 'kleo_framework' );
  34. $output .= "</div>";
  35. $output .= "</div>";
  36. echo $output;
  37. die();
  38. }
  39.  
  40. //if there are posts
  41. $post_types = array();
  42. $post_type_obj = array();
  43. foreach ( $posts as $post ) {
  44.  
  45. $post_types[ $post->post_type ][] = $post;
  46. if ( empty( $post_type_obj[ $post->post_type ] ) ) {
  47. $post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type );
  48. }
  49. }
  50.  
  51. foreach ( $post_types as $ptype => $post_type ) {
  52. if ( isset( $post_type_obj[ $ptype ]->labels->name ) ) {
  53. $output .= "<h4>" . $post_type_obj[ $ptype ]->labels->name . "</h4>";
  54. } else {
  55. $output .= "<hr>";
  56. }
  57. foreach ( $post_type as $post ) {
  58. $format = get_post_format( $post->ID );
  59. if ( get_the_post_thumbnail( $post->ID, 'thumbnail' ) ) {
  60. $image = get_the_post_thumbnail( $post->ID, 'thumbnail' );
  61. } else {
  62. if ( $format == 'video' ) {
  63. $image = "<i class='icon icon-film'></i>";
  64. } elseif ( $format == 'image' || $format == 'gallery' ) {
  65. $image = "<i class='icon icon-picture'></i>";
  66. } else {
  67. $image = "<i class='icon-info-sign'></i>";
  68. }
  69. }
  70.  
  71. $excerpt = "";
  72.  
  73. if ( ! empty( $post->post_content ) ) {
  74. $excerpt = "<br>" . char_trim( trim( strip_tags( strip_shortcodes( $post->post_content ) ) ), 40, "..." );
  75. }
  76. $link = apply_filters( 'kleo_custom_url', get_permalink( $post->ID ) );
  77. $classes = "format-" . $format;
  78. $output .= "<div class ='kleo_ajax_entry $classes'>";
  79. $output .= "<div class='ajax_search_image'>$image</div>";
  80. $output .= "<div class='ajax_search_content'>";
  81. $output .= "<a href='$link' class='search_title'>";
  82. $output .= get_the_title( $post->ID );
  83. $output .= "</a>";
  84. $output .= "<span class='search_excerpt'>";
  85. $output .= $excerpt;
  86. $output .= "</span>";
  87. $output .= "</div>";
  88. $output .= "</div>";
  89. }
  90. }
  91.  
  92. $output .= "<a class='ajax_view_all' href='" . home_url( '?s=' . $_REQUEST['s'] ) . "'>" . __( 'View all results', 'kleo_framework' ) . "</a>";
  93.  
  94. echo $output;
  95. die();
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement