This is the code in my searchform.php:
<div class="search-box">
<form method="get" class="searchform" action="<?php echo esc_url( home_url( "https://wordpress.stackexchange.com/" ) ); ?>">
<label for="s" class="assistive-text"><?php _e( 'Search', 'reddle' ); ?></label>
<div class="search-input-holder">
<input type="text" class="field s" name="s" value="Search..." onfocus="if (this.value == 'Search...') {this.value="";}" onblur="if (this.value == '') {this.value="Search...";}" />
</div>
<input type="image" src="https://wordpress.stackexchange.com/images/search.png" alt="Search" class="submit" name="submit" class="searchsubmit" />
</form>
</div>
And I use this to call the search box in my templates: <?php get_search_form(); ?>
I would like to use something like value="<?php the_search_query(); ?>"
instead of value="Search..."
, so that on search results pages, the search box is filled with the “search query” instead of the default placeholder text.
The problem with using value="<?php the_search_query(); ?>"
is that the search box is blank on all other pages (i.e. there’s no placeholder text).
Is there a way to set the default for the_search_query();
so that when it’s not a search results page some default text is shown? For example, something along the lines of value="<?php the_search_query( 'Search...' ); ?>"
(DOESN’T WORK!)
As of now, this is how I am doing it:
<div class="search-box">
<form method="get" class="searchform" action="<?php echo esc_url( home_url( "https://wordpress.stackexchange.com/" ) ); ?>">
<label for="s" class="assistive-text"><?php _e( 'Search', 'reddle' ); ?></label>
<div class="search-input-holder">
<?php if ( is_search() ) { ?>
<input type="text" class="field s" name="s" value="<?php the_search_query(); ?>" onfocus="if (this.value == 'Search...') {this.value="";}" onblur="if (this.value == '') {this.value="Search...";}" />
<?php } else { ?>
<input type="text" class="field s" name="s" value="Search..." onfocus="if (this.value == 'Search...') {this.value="";}" onblur="if (this.value == '') {this.value="Search...";}" />
<?php } ?>
</div>
<input type="image" src="https://wordpress.stackexchange.com/images/search.png" alt="Search" class="submit" name="submit" class="searchsubmit" />
</form>
</div>