我想出来了!以下是我使用的代码:
添加到functions.php
:
add_action( \'wp_ajax_nopriv_load-filter2\', \'prefix_load_term_posts\' );
add_action( \'wp_ajax_load-filter2\', \'prefix_load_term_posts\' );
function prefix_load_term_posts () {
$term_id = $_POST[ \'term\' ];
$args = array (
\'term\' => $term_id,
\'posts_per_page\' => -1,
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'yourtaxonomyhere\',
\'field\' => \'id\',
\'terms\' => $term_id,
\'operator\' => \'IN\'
)
)
);
global $post;
$myposts = get_posts( $args );
ob_start (); ?>
<ul class="list">
<?php foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php echo get_post_meta($post->ID, \'image\', $single = true); ?></a><br />
<?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}
jQuery脚本:
<script>
function term_ajax_get(termID) {
jQuery("a.ajax").removeClass("current");
jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
jQuery("#loading-animation").show();
var ajaxurl = \'http://yourdomain.com/wp-admin/admin-ajax.php\';
jQuery.ajax({
type: \'POST\',
url: ajaxurl,
data: {"action": "load-filter2", term: termID },
success: function(response) {
jQuery("#category-post-content").html(response);
jQuery("#loading-animation").hide();
return false;
}
});
}
</script>
我没有使用函数来列出类别,我只是分别列出每个类别。将数字替换为术语的ID:
<ul class="nav">
<li id="term-166"><a class="yourtermname ajax" onclick="term_ajax_get(\'166\');" href="#">Your Term Name</a></li>
<li id="term-354"><a class="yourtermname ajax" onclick="term_ajax_get(\'354\');" href="#">Your Term Name</a></li>
</ul>
此外,如果要筛选标记而不是术语,请替换:
\'term\'
具有\'tag__in\'
,$term_id
具有$tag_id
和变化\'taxonomy\' => \'yourtaxonomyhere\'
到\'taxonomy\' => \'post_tag\'
.