好的,这里有一个尝试,但是我不知道你的类别分类法的确切结构,所以它可能需要一些轻微的调整。我已经更新了表单的HTML,以便它使用用户选择的值,然后有一块PHP代码运行自定义WP_Query
要显示相关帖子(您可以编辑这些帖子以显示您喜欢的方式,目前它只返回无序的帖子列表)
HTML表单
<!-- THE HTML FOR THE FORM -->
<form action="" method="post">
<div class="customsearchul">
<div id="searchArea">
<ul>
<?php // Get the values from $_POST for area
if($_POST[\'area\']) { $a=$_POST[\'area\'];} else { $a=\'\';} ?>
<li><h4>Area:</h4></li>
<li>All of London<input type="radio" name="area" value="london" <?php if ($a == \'london\' || ! isset($_POST[\'area\']) ) { echo \'checked\'; } ?> /></li>
<li>North<input type="radio" name="area" value="north" <?php if ($a == \'north\' ) { echo \'checked\'; } ?> /> </li>
<li>East<input type="radio" name="area" value="east" <?php if ($a == \'east\') { echo \'checked\'; } ?> /> </li>
<li>South<input type="radio" name="area" value="south" <?php if ($a == \'south\') { echo \'checked\'; } ?> /></li>
<li>West<input type="radio" name="area" value="west" <?php if ($a == \'west\') { echo \'checked\'; } ?> /></li>
<li>Central<input type="radio" name="area" value="central" <?php if ($a == \'central\') { echo \'checked\'; } ?> /></li>
</ul>
</div>
</div>
<div class="customsearchul">
<div id="searchMusic">
<ul>
<?php // Get the values from $_POST for music
if($_POST[\'music\']) { $m=$_POST[\'music\'];} else { $m[]=\'\';} ?>
<li><h4>Music:</h4></li>
<li>House<input type="checkbox" name="music[]" value="house" <?php if (in_array(\'house\', $m)) { echo \'checked\'; } ?> /></li>
<li>Techno<input type="checkbox" name="music[]" value="techno" <?php if (in_array(\'techno\', $m)) { echo \'checked\'; } ?> /></li>
<li>Trance<input type="checkbox" name="music[]" value="trance" <?php if (in_array(\'trance\', $m)) { echo \'checked\'; } ?> /></li>
<li>Electronica<input type="checkbox" name="music[]" value="electronica" <?php if (in_array(\'electronica\', $m)) { echo \'checked\'; } ?> /></li>
<li>Drum and Bass<input type="checkbox" name="music[]" value="drum-and-bass" <?php if (in_array(\'drum-and-bass\', $m)) { echo \'checked\'; } ?> /></li>
<li>Garage<input type="checkbox" name="music[]" value="garage" <?php if (in_array(\'garage\', $m)) { echo \'checked\'; } ?> /></li>
<li>Dubstep<input type="checkbox" name="music[]" value="dubstep" <?php if (in_array(\'dubstep\', $m)) { echo \'checked\'; } ?> /></li>
<li>Trap<input type="checkbox" name="music[]" value="trap" <?php if (in_array(\'trap\', $m)) { echo \'checked\'; } ?> /></li>
<li>Hip-Hop<input type="checkbox" id="music" name="music[]" value="hiphop" <?php if (in_array(\'hiphop\', $m)) { echo \'checked\'; } ?> /></li>
<li>R\'n\'B<input type="checkbox" name="music[]" value="rnb" <?php if (in_array(\'rnb\', $m)) { echo \'checked\'; } ?> /></li>
<li>Rock<input type="checkbox" name="music[]" value="rock" <?php if (in_array(\'rock\', $m)) { echo \'checked\'; } ?> /></li>
<li>Indie<input type="checkbox" name="music[]" value="indie" <?php if (in_array(\'indie\', $m)) { echo \'checked\'; } ?> /></li>
<li>Reggae<input type="checkbox" name="music[]" value="reggae" <?php if (in_array(\'reggae\', $m)) { echo \'checked\'; } ?> /></li>
<li>Retro 80\'s/90\'s<input type="checkbox" name="music[]" value="retro" <?php if (in_array(\'retro\', $m)) { echo \'checked\'; } ?> /></li>
<li>Party Bangers<input type="checkbox" name="music[]" value="party-bangers" <?php if (in_array(\'party-bangers\', $m)) { echo \'checked\'; } ?> /></li>
<li>Chart Hits<input type="checkbox" name="music[]" value="chart-hits" <?php if (in_array(\'chart-hits\', $m)) { echo \'checked\'; } ?> /></li>
</ul>
</div>
</div>
<div class="customsearchul">
<div id="searchType">
<ul>
<?php // Get the values from $_POST for music
if($_POST[\'occasion\']) { $o=$_POST[\'occasion\'];} else { $o[]=\'\';} ?>
<li><h4>Search Type:</h4></li>
<!--<li>Everything<input type="checkbox" name="occasion[]" value="debauchery" /></li> -->
<li>Nightclubs<input type="checkbox" name="occasion[]" id="nightclub" value="nightclub" <?php if (in_array(\'nightclub\', $o)) { echo \'checked\'; } ?> /></li>
<li>Events & Tickets<input type="checkbox" name="occasion[]" value="club-guide" <?php if (in_array(\'club-guide\', $o)) { echo \'checked\'; } ?> /> </li>
<li>Live Venues<input type="checkbox" name="occasion[]" value="festivals" <?php if (in_array(\'festivals\', $o)) { echo \'checked\'; } ?> /></li>
<li>Artist Interviews<input type="checkbox" name="occasion[]" value="artist" <?php if (in_array(\'artist\', $o)) { echo \'checked\'; } ?> /></li>
</ul>
</div>
</div>
<input class="searchbutton" type="submit" value="Search" id="performsearch"/>
</form>
PHP代码(WP\\U查询)
<?php
// Get the values from $_POST
$a = ($_POST[\'area\']);
$m = ($_POST[\'music\']);
$o = ($_POST[\'occasion\']);
// Change the ID of the area category
$a_tmp = get_category_by_slug( $a );
$a_catID[] = $a_tmp->term_id;
// Get the ID of each music category
// Do some conditional checks to make sure
// that we\'re correctly indexing an array
// and that output is an array for the later
// use with array_merge()
if ( is_array($m) ) {
// If we already have an array it means
// multiple checkboxes have been selected
foreach( $m as $m_cat ){
$m_tmp = get_category_by_slug( $m_cat );
$m_catIDs[] = $m_tmp->term_id;
}
} else {
// If we don\'t have an array then
// only one checkbox has been
// selected
$m_tmp = get_category_by_slug( $m );
$m_catIDs[] = $m_tmp->term_id;
}
// Get the ID of each occasion category
// with the checking as described above
if ( is_array($o) ) {
foreach( $o as $o_cat ){
$o_tmp = get_category_by_slug( $o_cat );
$o_catIDs[] = $o_tmp->term_id;
}
} else {
$o_tmp = get_category_by_slug( $o );
$o_catIDs[] = $o_tmp->term_id;
}
// Concatenate the arrays
$cat_IDs = array_merge($a_catID,$m_catIDs,$o_catIDs);
// Build the WP_Query arguments
$query_args =
array (
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
//\'category__and\' => $cat_IDs
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'term_id\',
\'operator\' => \'AND\',
\'terms\' => $cat_IDs
),
)
);//end $query_args;
$the_query = new WP_Query($query_args);
// The Loop to echo posts
if ( $the_query->have_posts() ) {
// if we have posts, echo opening <ul>
echo \'<ul>\';
while ( $the_query->have_posts() ) {
// while we have posts, echo the title in an <li>
$the_query->the_post();
echo \'<li>\' . get_the_title() . \'</li>\';
}
// echo the closing <ul>
echo \'</ul>\';
} else {
// no posts found
echo \'Nothing found.\';
} wp_reset_postdata(); /* Restore original Post Data */
?>
几点提示:
您需要仔细检查分类设置,并确保调用get_category_by_slug
正在传递类别的slug可能会更改从循环输出的HTML,因此它不仅仅是<ul>