open
05.10.2022.

Search results example – The search.php

Last update: 05.11.2024.

Create a file named search.php. Insert the following code and upload it to the current template directory.

For the live demo, search for a word in the sidebar.

/// search.php
<?php get_header(); ?>
<?php $s=get_search_query();$args = array('s' =>$s);$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) { _e("<h2 class='searchresults'>Search results for ".get_query_var('s')." word</h2>");?>
<?php while ( $the_query->have_posts() ) {$the_query->the_post();?>

<?php }}else{?>
<h2>No results</h2>
<?php } ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<h1><a href="<?php the_permalink() ?>" title="<?php the_title();?>"><?php the_title();?></a></h1>

<?php endwhile;endif; ?>
<?php
the_posts_pagination( array(
'prev_text' => __( '<i class="fa fa-arrow-left"></i>', 'textdomain' ),
'next_text' => __( '<i class="fa fa-arrow-right"></i>', 'textdomain' ),
) );
?>
<?php get_sidebar(); ?>

<?php get_footer(); ?>

Copy to Clipboard


Related posts

Got a question? Is something not working? Write and I'll answer!

Leave a Reply

Your email address will not be published. Required fields are marked *