我试图在WordPress中创建一个搜索过滤器,以过滤自定义帖子类型中的所有自定义分类。
我已经创建了一个生成下拉列表的函数,这样我就可以将每个选项值作为一个slug输出给我已经在工作的过滤器。我唯一的问题是“全选”选项不起作用。
选择“全部”时,返回的结果没有找到任何内容。过滤器可以是here.
我用来创建函数的代码是:
function adopt_custom_taxonomy_dropdown( $taxonomy, $orderby = \'date\', $order = \'DESC\', $limit = \'-1\', $name, $show_option_all = null, $show_option_none = null ) {
$args = array(
\'orderby\' => $orderby,
\'order\' => $order,
\'number\' => $limit,
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
printf( \'<select name="%s" class="postform">\', esc_attr( $name ) );
if ( $show_option_all ) {
printf( \'<option value="0">%s</option>\', esc_html( $show_option_all ) );
}
if ( $show_option_none ) {
printf( \'<option value="-1">%s</option>\', esc_html( $show_option_none ) );
}
foreach ( $terms as $term ) {
printf( \'<option value="%s">%s</option>\', esc_attr( $term->slug ), esc_html( $term->name ) );
}
print( \'</select>\' );
}
}
这就是结果被拉到。。。
<?php
if (isset($_GET["farm-type"]) && empty($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$myquery1[\'tax_query\'] = array(
array(
\'taxonomy\' => \'farm-type\',
\'terms\' => array($farm_type),
\'field\' => \'slug\',
),
);
query_posts($myquery1);
}
?>
<?php
if (isset($_GET["farm-type"]) && isset($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$farm_location = $_GET["location-farms"];
$myquery2[\'tax_query\'] = array(
array(
\'taxonomy\' => \'farm-type\',
\'terms\' => array($farm_type),
\'field\' => \'slug\',
),
array(
\'taxonomy\' => \'location-farms\',
\'terms\' => array($farm_location),
\'field\' => \'slug\',
),
);
query_posts($myquery2);
}
?>