php 在 Wordpress 主题中(使用 get_search_form()),如何将占位符文本添加到搜索框中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19870329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In a Wordpress theme (using get_search_form()), how can I add placeholder text to the search box?
提问by BrianLender
I am trying to create a theme and I am displaying a search box in the header using:
我正在尝试创建一个主题,并使用以下方法在标题中显示一个搜索框:
<?php get_search_form(); ?>
Is there a way that I can get placeholder text to show in that box?
有没有办法让占位符文本显示在那个框中?
回答by BrianLender
I ended up figuring out that if I don't have a file called "searchform.php" in my theme folder, Wordpress displays is default form that doesn't include any placeholder text:
我最终发现如果我的主题文件夹中没有名为“searchform.php”的文件,Wordpress 显示的默认表单不包含任何占位符文本:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
I created a new file called "searchform.php" in my theme folder and modified the default form to include the placeholder text:
我在我的主题文件夹中创建了一个名为“searchform.php”的新文件,并修改了默认表单以包含占位符文本:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div>
<input type="text" value="" name="s" id="s" placeholder="Search gear, clothing, & more.." />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
回答by Vishal Thoriya
make searchform.php in theme folder and place that code in it
在主题文件夹中制作 searchform.php 并将该代码放入其中
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>">
<div>
<input class="text" type="text" value=" " name="s" id="s" />
<input type="submit" class="submit button" name="submit" value="<?php _e('Search');?>" />
</div>
</form>
Change whatever you want ! like class,placeholder,text any thing you want but not change like name, id and form attribute
随心所欲改变!像类,占位符,文本任何你想要但不改变的东西,如名称、ID 和表单属性
Thanks
谢谢
回答by Tobias
The WP codex has an example with a placeholder:
If your theme supports HTML5, which happens if it uses
add_theme_support('html5', array('search-form'))
, it will output the following HTML form. This is the case since WordPress 3.6.
如果您的主题支持 HTML5,如果使用
add_theme_support('html5', array('search-form'))
就会发生这种情况 ,它将输出以下 HTML 表单。自 WordPress 3.6 以来就是这种情况。
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text">Search for:</span>
<input type="search" class="search-field" placeholder="Search …" value="" name="s" title="Search for:" />
</label>
<input type="submit" class="search-submit" value="Search" />
</form>
If you don't want to use HTML5 tags and attributes, you can create placeholders with JavaScript.
如果您不想使用 HTML5 标签和属性,您可以使用 JavaScript 创建占位符。