我有一个页面,我想在上面显示特定自定义帖子类型的所有帖子,我们称之为“persons”。我想允许用户按多个(!)筛选所有人员分类:年龄、城市、职业、经验水平。最好的方法是单击复选框并通过ajax立即获得结果,而无需重新加载任何页面。
我认为这是许多Wordpress开发人员想要实现的。不幸的是,我还没有找到任何合适的教程来实现这一点。但对于有经验的WordPress开发人员来说,这一定很容易做到。。。
任何人都可以提供一个代码模板来说明如何为上述自定义帖子类型构建过滤器页面吗?
我已经知道如何通过jQuery这一分类法来过滤帖子。这是我目前使用的代码:
<!-- PAGE TEMPLATE FOR OVERVIEW OF ALL POSTS OF CUSTOM POST TYPE "PERSONS" -->
<!-- LIST OF ALL ENTRIES OF TAXONOMY "CITIES" -->
<div class="cities">
<a href="#" id="all">All cities</a>
<?php
// Get all terms of a taxonomy
$taxonomy = \'cities\';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) { ?>
<a href="javascript:void(0);" data-target="<?php echo $term -> slug; ?>"><?php echo $term -> name; ?></a>
<?php } endif; ?>
</div>
<!-- OUTPUT PERSONS -->
<div class="overview_persons">
<?php
$args= array(
\'post_type\' => \'persons\',
\'post_status\' => \'publish\',
\'meta_key\' => \'zip\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1
);
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post();
$post_terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "slugs" ) );
$post_terms_space_separated = implode(" ", $post_terms);
?>
<!-- output selected taxonomy value in class of person div -->
<div class="person <?php echo $post_terms_space_separated; ?>">
<!-- description of person -->
</div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
<!-- MY JQUERY FOR FILTERING PERSONS VIA TAXONOMY "CITY" -->
<script>
jQuery(document).ready(function() {
// Click on a city
jQuery(\'a[data-target]\').click(function() {
jQuery(\'.person\').show().not(\'.\' + this.dataset.target).hide();
});
// Click on "All cities"
jQuery(\'#all\').click(function(e) {
e.preventDefault();
jQuery(\'.person\').show();
});
});
</script>
这段代码运行得非常好。但我面临的新挑战是通过不止一种分类法来筛选人——不仅是根据城市,而且还根据年龄、职业和经验水平。我需要如何修改代码来实现这一点?当然,通过Ajax实现这一点将是更可取的方式。