How do I write a custom search.php results page
-
Hi.
I’m trying to build a version of the wordpress search results page.
I’ve tried to find a basic example of the loop I need to put on the page to display the results, but after Googling, I can’t find a straight forward answer.
What I’ve currently written clearly has errors in the code because it will only render 1 result on each paginated page. I want it to return 25 results per page from a specific category (165).
I’ve discovered that when I remove infinite scroll the paginated results are in fact only displaying 12 posts when there are actually 36 in category 165.
To compound the problem I am trying to use Metafizy’s Isotope filtering and Jquery infinite scroll to display my results, which is making my code hard to follow.
I think the problem is in the fact I’m using query_posts to display my results, but this new WP_Query I’ve written further down the page (see code).
All in all, I’m quite confused at this point. I’d really appreciate it if someone could provide me with the simple loop I need to add to search.php in my theme so it will render all 36 results from category 165 with the results being paginated with each paginated page displaying 25 posts.
Here is my current code.
<?php /** * The template for displaying Search Results pages. * * @package WordPress * @subpackage pBone */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } get_header(); ?> <div id="wrapper" class="container d-sm-flex"> <div class=""> <?php query_posts( array('paged' => get_query_var('paged'), 'post_type' => array( 'post'), 's' => $s ) ); if ( have_posts() ): ?> <h2>Search results for: <?php echo get_search_query(); ?></h2> <div class="filters" id="collapseExample"> <div class="ui-group"> <p class="text-monospace m-0">Choose an instrument</p> <div class="button-group js-radio-button-group d-flex flex-wrap" data-filter-group="instrument"> <button class="btn btn-primary button is-checked mr-1 mb-1" type="button" data-filter="">any</button> <?php $terms = get_terms( 'category', array('parent' => 165)); // get all categories, but you can use any taxonomy $count = count($terms); //How many are they? if ( $count > 0 ){ //If there are more than 0 terms foreach ( $terms as $term ) { //for each term: echo "<button class='button btn btn-primary mr-1 mb-1' type='button' data-filter='.".$term->slug."'>" . $term->name . "</button>\n"; //create a list item with the current term slug for sorting, and name for label } } ?> </div> </div> <div class="ui-group"> <p class="text-monospace m-0">Pick your resource type</p> <div class="button-group js-radio-button-group d-flex flex-wrap" data-filter-group="type"> <button class="button is-checked btn btn-primary mr-1 mb-1" type="button" data-filter="">any</button> <?php $field_key = "field_5ed77f279f5e2"; $field = get_field_object($field_key); if( $field ) { foreach( $field['choices'] as $k => $v ) { echo '<button class="button btn btn-primary mr-1 mb-1" type="button" data-filter=".' . $k . '">' . $v . '</button>'; } } ?> </div> </div> </div> <?php $terms_ID_array = array(); foreach ($terms as $term) { $terms_ID_array[] = $term->term_id; // Add each term's ID to an array } $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $terms_ID_string = implode(',', $terms_ID_array); // Create a string with all the IDs, separated by commas //$the_query = new WP_Query( 'posts_per_page=3&cat='.$terms_ID_string ); // Display 50 posts that belong to the categories in the string $the_query = new WP_Query ( array( //'cat' => $terms_ID_string, 'posts_per_page' => 25, 'paged' => $paged )); ?> <?php if ( $the_query->have_posts() ) : ?> <div id="Grid" class="article-feed grid d-md-flex pt-4"> <?php while ( have_posts() ) : the_post(); $termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item $termsString = ""; //initialize the string that will contain the terms foreach ( $termsArray as $term ) { // for each term $termsString .= $term->slug.' '; //create a string that has all the slugs } ?> <?php $colors = get_field('content-type'); if( $colors ): ?> <?php foreach( $colors as $color ): ?> <?php $color = strtolower($color); ?> <?php $color = str_replace(' ', '-', $color); ?> <div data-src="<?php echo $color; ?>" class="article infinite-item item <?php echo $termsString;?> <?php echo $color; ?> col-md-6 col-lg-4 mb-4 hidden-opacity"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?> <div class="c-opus__box u-box-shadow card "> <?php if ( has_post_thumbnail() ) : ?> <div class="thumbnail"> <span> <div class="isotope-im-wrapper col d-flex align-items-center justify-content-center p-0 pt-4 pt-md-0"> <?php $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ); ?> <!-- Standard version --> <img class="img-fluid card-img-top" src="<?php the_post_thumbnail_url(); ?>"/> </div> </span> </div> <?php endif; ?> <div class="card-body "> <h3 class="u-h4 c-opus__title u-margin-bottom-small card-title"><?php the_title(); ?></h3> <div class="o-layout"> <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...');?> </div> <?php if(get_field('hubspot_cta_embed_code')): ?> <?php the_field('hubspot_cta_embed_code'); ?> <?php endif; ?> <?php if(get_field('keyword_tags')): ?> <div class="keyword-tags"> <hr class="my-4"> <p class="lead"><strong>Tags</strong></p> <?php the_field('keyword_tags'); ?> </div> <?php endif; ?> </div> </div> <?php endforeach; ?> </div> </div><!-- end isotope-list --> <?php else : ?> </div><!-- end isotope-list --> <?php endif; ?> <?php endwhile; ?> <!-- status elements --> <div class="scroller-status" style="display: block;"> <div class="loader-ellips infinite-scroll-request" style="display: none;"> <span class="loader-ellips__dot"></span> <span class="loader-ellips__dot"></span> <span class="loader-ellips__dot"></span> <span class="loader-ellips__dot"></span> </div> <p class="scroller-status__message infinite-scroll-last" style="display: block;">End of content</p> <div class="scroller-status__message infinite-scroll-error" style="display: none;"><p>No results found</p></div> </div> <p class="pagination"> <?php previous_posts_link( 'Older posts' ); ?> <?php next_posts_link( 'Newer posts' ); ?> </p> <?php wp_reset_postdata(); ?> <?php else: ?> <h2>No search results for: <?php echo get_search_query(); ?></h2> <?php endif; ?> </div><!-- #wrapper --> </div> <?php else : ?> No results found please use a different search term. </div> </div> <?php endif; ?> <?php get_footer(); ?>
The page I need help with: [log in to see the link]
- The topic ‘How do I write a custom search.php results page’ is closed to new replies.