我正在使用ACF(高级自定义字段)并使用他们在网站上发布的方法过滤结果。
http://www.advancedcustomfields.com/resources/tutorials/creating-wp-archive-custom-field-filter/
过滤工作正常,但显示结果时,顶部菜单消失。我使用的代码如下:
在函数中。php add\\u action(\'pre\\u get\\u posts\',\'my\\u pre\\u get\\u posts\');
函数my\\u pre\\u get\\u posts($query){//验证if(is\\u admin()){返回;}
// get original meta query
$meta_query = $query->get(\'meta_query\');
// allow the url to alter the query
// eg: http://www.website.com/events?location=melbourne
// eg: http://www.website.com/events?location=sydney
if( !empty($_GET[\'bedrooms\']) )
{
$bedrooms = explode(\',\', $_GET[\'bedrooms\']);
//Add our meta query to the original meta queries
$meta_query[] = array(
\'key\' => \'bedrooms\',
\'value\' => $bedrooms,
\'compare\' => \'IN\',
);
}
// update the meta query args
$query->set(\'meta_query\', $meta_query);
// always return
return;
}
在类别中。php
<div id="search-houses">
<?php
$field = get_field_object(\'bedrooms\');
$values = explode(\',\', $_GET[\'bedrooms\']);
?>
<ul>
<?php foreach( $field[\'choices\'] as $choice_value => $choice_label ): ?>
<li>
<input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
</li>
<?php endforeach; ?>
</ul>
</div>
<script type="text/javascript">
(function($) {
$(\'#search-houses\').on(\'change\', \'input[type="checkbox"]\', function(){
// vars
var $ul = $(this).closest(\'ul\'),
vals = [];
$ul.find(\'input:checked\').each(function(){
vals.push( $(this).val() );
});
vals = vals.join(",");
window.location.replace(location.pathname + \'?bedrooms=\' + vals);
console.log( vals );
});
})(jQuery);
</script>
演示:
http://thepursesociety.com/_web/category/houses