在同一页面上为搜索输入设置不同的占位符


Have different placeholders for search inputs on the same page - wordpress

如果在wordpress中使用searchform.php来显示搜索输入框。如何在同一页面的不同搜索框中显示不同的占位符?例如,我有一个页面,在页脚、侧边栏的小部件和主导航菜单上都有一个搜索框。我希望在每个位置显示不同的占位符

这是我的搜索表

<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
    <input id="s" placeholder="Search..." type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';" 
    onblur="if (this.value=='') this.value = this.defaultValue" >
    <input class="searchsubmit" type="submit" value="<?php _e( '', 'mytheme' ); ?>">
</form>
<!-- /Searchform -->

我真的不知道你想做什么,但一般的想法是相当简单的。只需有条件地创建占位符文本。

if (is_home()) {
  $placeholder = "Search Posts...";
} elseif(is_tag()) {
  $placeholder = "Search Tags...";
} else {
  $placeholder = "Search...";
}
<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
    <input id="s" placeholder="<?php echo $placeholder; ?>" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';" 

这是你正在寻找的东西吗?

基于对需求的进一步解释

搜索表单功能并不像你想的那样工作。你可以用全局变量来欺骗它。

$GLOBALS['placeholder'] = "Default placeholder value";
functions.php

,然后编辑您的表单为…

<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
    <input id="s" placeholder="<?php echo $GLOBALS['placeholder']; ?>" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';" 

并像这样调用函数:

$GLOBALS['placeholder'] = "Placeholder value...";
get_search_form();

根据文档,get_search_form(false)应该返回一个字符串,这意味着这样的事情应该工作:

编辑搜索表单为:

<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
    <input id="s" placeholder="%s" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';" 

然后使用…

$search_form = get_search_form(false);
printf($search_form,'Placeholder...');

避免了全局变量,这通常是一件好事。我不能在盒子里工作。如果你看一下来源,这个功能似乎取决于searchform.php的存在与否。如果该文件存在,则只是包含它,这使得"返回字符串"部分相当危险。事实证明,使用可用的过滤器来劫持它非常困难。

为了避免global杂耍,我认为您需要替换' get_search_form',例如,用:

function my_get_searchform($placeholder = 'Search') {
  $search_form_template = locate_template('searchform.php');
  if ( '' != $search_form_template ) {
    ob_start();
    require($search_form_template);
    $form = ob_get_clean();
    printf($form,$placeholder);
  }
}

get_search_form()函数打印出searchform.php中的代码。不要在页面中使用get_search_form(),而是取出该函数并从searchform.php中复制代码,并将其粘贴到希望显示搜索栏的位置。然后相应地更改占位符文本。

<html>
    <head>
        <!-- Header Stuff -->
    </head>
    <body> 
        <div class="main-navigation-menu">   
            <!-- Searchform -->
            <form method="get" class="search" action="<?php echo home_url(); ?>" >
                <input id="s" placeholder="PLACEHOLDER TEXT HERE" type="text" name="s" onfocus="if    (this.value==this.defaultValue) this.value = '';" 
                onblur="if (this.value=='') this.value = this.defaultValue" >
                <input class="searchsubmit" type="submit" value="<?php _e( '', 'mytheme' ); ?>">
            </form>
            <!-- /Searchform -->
        </div>
        <div class="sidebar">
           <div class="widget">
               <!-- Default widget stuff from functions.php will appear here. Searchform styling will come from searchform.php -->
           </div>
        </div>
        <div class="footer">   
            <!-- Searchform -->
            <form method="get" class="search" action="<?php echo home_url(); ?>" >
                <input id="s" placeholder="PLACEHOLDER TEXT HERE" type="text" name="s" onfocus="if    (this.value==this.defaultValue) this.value = '';" 
                onblur="if (this.value=='') this.value = this.defaultValue" >
                <input class="searchsubmit" type="submit" value="<?php _e( '', 'mytheme' ); ?>">
            </form>
            <!-- /Searchform -->
        </div>
    </body>
</html>

保留searchform.php文件,并按照您希望的小部件搜索表单样式显示。