Advertisement
Guest User

search template

a guest
Dec 13th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. <?php
  2. /**
  3. * The template for displaying search results pages.
  4. *
  5. *
  6. */
  7.  
  8.  
  9. global $query_string;
  10. global $wp_query;
  11.  
  12. $search_query = wp_parse_str( $query_string, $array );
  13. $search = new WP_Query( $search_query );
  14. $total_results = $wp_query->found_posts;
  15.  
  16.  
  17. // Create dummy post (not in database) to avoid error messages due to NULL post object
  18. if ( $total_results == 0 ) {
  19. global $post;
  20.  
  21. // WP_Post constructor
  22. function __construct( $post ) {
  23. foreach ( get_object_vars( $post ) as $key => $value )
  24. $this->$key = $value;
  25. }
  26.  
  27. // set up attributes for this dummy post
  28. $post_id = -99; // negative ID, to avoid clash with a valid post
  29. $post = new stdClass();
  30. $post->ID = $post_id;
  31. $post->post_author = 1;
  32. $post->post_date = current_time( 'mysql' );
  33. $post->post_date_gmt = current_time( 'mysql', 1 );
  34. $post->post_title = 'Some dummy title';
  35. $post->post_content = 'Some dummy content';
  36. $post->post_status = 'publish';
  37. $post->comment_status = 'open';
  38. $post->post_name = 'fake-page-' . rand( 1, 99999 ); // append random number to avoid clash
  39. $post->post_type = 'page';
  40. $post->filter = 'raw'; // important!
  41.  
  42. // Note: this gets rid of auto-pagination errors, but introduces comment-template notice that I can't seem to fix (Notice: Trying to get property of non-object in /var/www/html/wp-includes/comment-template.php on line 1196), which I believe refers to $post, which is now this dummy instead of some null post
  43. }
  44. // End dummy post creation
  45.  
  46.  
  47. get_header(); ?>
  48. <div class="search-container">
  49. <section id="primary" class="content-area">
  50. <main id="main" class="site-main" role="main">
  51. <div class="search-page-form" id="ss-search-page-form"><?php get_search_form(); ?></div>
  52.  
  53. <header class="page-header">
  54. <div class="search-page-title"><b>You searched for: </b><?php echo get_search_query(); ?></div>
  55. <div class="number-results">We found <?php echo $total_results ?> results.</div>
  56. </header><!-- .page-header -->
  57.  
  58. <?php if ( have_posts() ) : ?>
  59.  
  60. <?php /* Start the Loop */ ?>
  61. <?php while ( have_posts() ) : the_post(); ?>
  62. <?php
  63. global $post;
  64. $image = get_the_post_thumbnail_url( $post->ID, 'medium-large-size' );
  65. if( empty( $image ) )
  66. {
  67. $image = get_stylesheet_directory_uri()."/images/default.png";
  68. }
  69. ?>
  70. <div class="single-result-item">
  71. <div class="results-featured-image"><a href="<?php the_permalink(); ?>"><img src="<?php echo $image ?>" ></a></div>
  72. <div class="results-item-wrapper">
  73. <h3 class="search-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
  74. <div class="search-post-excerpt"><?php the_excerpt(); ?></div>
  75.  
  76.  
  77. <div class="button-wrapper"><button class="search-post-link"><a href="<?php the_permalink(); ?>">Read More</a></button></div>
  78. </div>
  79. </div>
  80.  
  81. <?php endwhile; ?>
  82.  
  83. <?php base_pagination(); ?>
  84.  
  85. <?php wp_reset_postdata(); // added this to bring query back if multiple queries are desired ?>
  86.  
  87. <?php else : ?>
  88.  
  89. <?php get_template_part( 'template-parts/content', 'none' ); ?>
  90.  
  91. <div class="archive-page">
  92. <br clear="all">
  93. <p>However, you might be interested in checking out these recent posts:</p>
  94.  
  95. <h3>Recent Posts</h3>
  96.  
  97. <?php echo do_shortcode('[display-posts post_type="waterfalls,post,userwaterfallreview" posts_per_page="25" orderby="date" order="DESC" image_size="medium" wrapper="div" include_author="true" include_date="true" date_format="F j, Y g:i a" include_excerpt="true" excerpt_length="55" excerpt_more="...Read More" excerpt_more_link="true" default_image="75483"]'); ?>
  98.  
  99. </div><!-- end .archive-page -->
  100.  
  101. <?php endif; ?>
  102.  
  103. </main><!-- #main -->
  104. </section><!-- #primary -->
  105. </div>
  106. <?php //get_sidebar(); ?>
  107. <?php get_footer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement