2013-03-02 44 views
0

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

这是我在searchform

<!-- 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 --> 

回答

0

已经得到了我真的不知道你正在尝试做的,但总的想法是很简单。只要有条件地创建占位符文本。

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); 
    } 
} 
+0

不完全,我有一个页面,在不同的位置有3个搜索框。其中一个很小,所以我不希望它有一个占位符。它应该是更多的地方,如果声明,但我不确定如何做到这一点。例如:如果搜索在#主菜单中,则不显示占位符 – user1202292 2013-03-02 16:15:56

0

的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和样式文件,表明您希望您的窗口小部件搜索窗体显示。

相关问题