因此,我有一个页面模板,设置如下(使用Kleo主题):
搜索模板
get_header(); ?>
<?php
//create right sidebar template
kleo_switch_layout(\'right\');
?>
<?php get_template_part(\'page-parts/general-title-section\'); ?>
<?php get_template_part(\'page-parts/general-before-wrap\'); ?>
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post(); ?>
<!-- Begin the Treeview menu -->
<form method="get" action="<?php bloginfo(\'url\'); ?>">
<div class="form-group">
<input class="form-control" type="text" name="s" value="" placeholder="Search…" maxlength="50" required="required" />
</div>
<p>Refine search to posts containing chosen tags:</p>
<div class="acidjs-css3-treeview">
<ul>
<li><input type="checkbox" id="node-0" /><label><input type="checkbox" name="tag[]" value="node-0" /><span></span></label><label for="node-0">node-0</label>
<ul>
<li><input type="checkbox" id="node-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0" /><span></span></label><label for="node-0-0">node-0-0</label>
<ul>
<li><input type="checkbox" id="node-0-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0-0" /><span></span></label><label for="node-0-0-0">node-0-0-0</label></li>
<li><input type="checkbox" id="node-0-0-1" /><label><input type="checkbox" name="tag[]" value="node-0-0-1" /><span></span></label><label for="node-0-0-1">node-0-0-1</label></li>
</ul>
</li>
</ul>
</li>
<li><input type="checkbox" id="node-1" /><label><input type="checkbox" name="tag[]" value="node-1" /><span></span></label><label for="node-1">node-1</label>
<ul>
<li><input type="checkbox" id="node-1-0" /><label><input type="checkbox" name="tag[]" value="node-1-0" /><span></span></label><label for="node-1-0">node-1-0</label>
<ul>
<li><input type="checkbox" id="node-1-0-0" /><label><input type="checkbox" name="tag[]" value="node-1-0-0" /><span></span></label><label for="node-1-0-0">node-1-0-0</label></li>
<li><input type="checkbox" id="node-1-0-1" /><label><input type="checkbox" name="tag[]" value="node-1-0-1" /><span></span></label><label for="node-1-0-1">node-1-0-1</label></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<!-- End the Treeview menu -->
<input class="btn btn-primary" type="submit" value="Submit" />
</form>
<?php
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( \'content\', \'page\' );
?>
<?php get_template_part( \'page-parts/posts-social-share\' ); ?>
<?php if ( sq_option( \'page_comments\', 0 ) == 1 ): ?>
<!-- Begin Comments -->
<?php comments_template( \'\', true ); ?>
<!-- End Comments -->
<?php endif; ?>
<?php endwhile;
endif;
?>
<?php get_template_part(\'page-parts/general-after-wrap\'); ?>
<?php get_footer(); ?>
问题搜索功能不正常。它返回如下查询字符串:
URL将显示http://example.com/?s=searchterm&tag[]=key-word1&tag[]=key-word2
这将导致标记未被过滤。
我可以编辑[]以使标记搜索正常工作,但这样我只会搜索GET中的最后一个标记元素,这就达不到目的了。
I was inspired to do this particular code for searching multiple tags on this question here. 这个链接似乎表明,在WP 4.4出现之前,这是行不通的。在当前WP 4.3.1下,是否有办法使其工作?
最合适的回答,由SO网友:David Avellan 整理而成
好吧,我通过制作自己的解析php来实现它,如下所示:
解析。php
<?php
$tags = $_POST[\'tag\'];
$search = $_POST[\'s\'];
$count = count($tags);
$i = 0;
if(!empty($search))
$uri = "https://example.com/?s=$search&";
else
$uri = "https://example.com/?tag=";
foreach($tags as $name=>$value) {
++$i;
if($i !== $count)
$uri .= $value."+";
else
$uri .= $value;
}
header("Location: $uri");
exit;
?>
当然,将原始表单操作更改为指向parse。php和发布方法。
也许这不是最好的方法,但现在它起作用了。如果有人能给我一个更好或更清晰的答案,请回答。