这是我搜索表单中的代码。php:
<div class="search-box">
<form method="get" class="searchform" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<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="/images/search.png" alt="Search" class="submit" name="submit" class="searchsubmit" />
</form>
</div>
我用它调用模板中的搜索框:
<?php get_search_form(); ?>
我想用value="<?php the_search_query(); ?>"
而不是value="Search..."
, 因此,在搜索结果页面上,搜索框将填充“搜索查询”,而不是默认占位符文本。
使用的问题value="<?php the_search_query(); ?>"
其他所有页面上的搜索框均为空(即没有占位符文本)。
有没有办法设置the_search_query();
所以当它不是搜索结果页面时,会显示一些默认文本?例如value="<?php the_search_query( \'Search...\' ); ?>"
(不起作用!)
到目前为止,我是这样做的:
<div class="search-box">
<form method="get" class="searchform" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<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="/images/search.png" alt="Search" class="submit" name="submit" class="searchsubmit" />
</form>
</div>