Skip to content

Instantly share code, notes, and snippets.

@Bobz-zg
Created August 12, 2016 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bobz-zg/16578c3c173abfe2b22daffd67f6f1e0 to your computer and use it in GitHub Desktop.
Save Bobz-zg/16578c3c173abfe2b22daffd67f6f1e0 to your computer and use it in GitHub Desktop.
Filter WP posts by tag - JS
jQuery(document).ready(function($) {
$('.tax-filter').click( function(event) {
// Prevent default action - opening tag page
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
// Get tag slug from title attirbute
var selecetd_taxonomy = $(this).attr('title');
// After user click on tag, fade out list of posts
$('.tagged-posts').fadeOut();
data = {
action: 'filter_posts', // function to execute
afp_nonce: afp_vars.afp_nonce, // wp_nonce
taxonomy: selecetd_taxonomy, // selected tag
};
$.post( afp_vars.afp_ajax_url, data, function(response) {
if( response ) {
// Display posts on page
$('.tagged-posts').html( response );
// Restore div visibility
$('.tagged-posts').fadeIn();
};
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment