Skip to content

Instantly share code, notes, and snippets.

@gstricklind
Last active January 25, 2018 01:38
Show Gist options
  • Save gstricklind/b1ebdc933ebf4613802c10f68067939a to your computer and use it in GitHub Desktop.
Save gstricklind/b1ebdc933ebf4613802c10f68067939a to your computer and use it in GitHub Desktop.
Add data attribute with variable term_id to wp_list_categories
<?php
/*========================================================================
Filter wp_list_categories to add new attribute
========================================================================*/
add_filter('wp_list_categories','moons_ajax_onclick');
function moons_ajax_onclick($output){
//if (is_post_type_archive('crew')) { // localize this filter if needed
//if not using a CPT then change get_terms to get_categories
$terms = get_terms( array(
'taxonomy' => 'crew_category',
));
// cycle through the terms
foreach($terms as $term) {
$term_id = $term->term_id;
$pattern = '/' . $term_id . '"><a/';
// onclick is the new attribute, use or change as needed
$replacement = $term_id . '"><a class="' . $term_id . '" onclick="term_ajax_get(\'' . $term->term_id . '\')" ';
$output = preg_replace( $pattern, $replacement, $output );
}
return $output;
/*} else {
return $output; //output for everything else
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment