This function takes a URL structure which is most often a query string (but sometimes not) and generates filters to remove query variables from the URL.
If you're looking at an archive like
you'll get the following output:
[ROCK X] [DEEP PURPLE X]
so you can remove those "filters".
I have a lot (too much maybe) of nested structure-- if anyone has any general optimization or refactoring pointers for me I'd really appreciate it!
<?php
function event_filters() {
$output = '';
$filterclear = get_bloginfo('wpurl').'/events';
$filterclass = 'filter-clear cp block fl search-tag tdn pr con ic-right ic-x c2h btn bg1 c2';
$queryString = $_SERVER['QUERY_STRING'];
$home = get_bloginfo('wpurl');
parse_str($queryString, $params);
$filter_count = count($params);
// IF IS SINGLE DAY VIEW
if(tribe_is_day()) {
$day_title = ucwords(strftime( '%e de %B ', strtotime( get_query_var('eventDate') )));
$output .= '<a class="'.$filterclass.'" title="Remove filter: ' . $day_title . '" href="'.$filterclear.'">'.$day_title.'</a>';
}
foreach ($params as $key => $term) {
$tags = explode(' ',$term);
$tag_count = count($tags);
$term = get_term_by('slug', $term, $key);
$term_name = $term->name;
if($filter_count == 1 && $tag_count <= 1) {
// IF IS ONE KEY AND ONE TERM
$output .= '<a class="'.$filterclass.'" title="Remove filter: ' . $term_name . '" href="'.$filterclear.'">'.$term_name.'</a>';
} else {
if($tag_count > 1) {
foreach($tags as $tag) {
// IF THERE IS MORE THAN ONE TERM PER KEY, LOOP THROUGH EACH TERM
$the_tag = get_term_by('slug', $tag, $key);
$tag_name = $the_tag->name;
$filterclear = str_replace($tag, '', $queryString);
$filterclear = str_replace(array('=+', '++', '+&'), array('=', '+', '&'), $filterclear);
if(substr($filterclear, -1) == '+'){
$filterclear = substr($filterclear,0,-1);
}
$output .= '<a class="'.$filterclass.'" title="Remove filter: ' . $tag_name . '" href="' . $home . '?'.$filterclear . '">'.$tag_name.'</a>';
}
} else {
// OR ELSE THERES JUST ONE TERM PRESENT
$filterclear = remove_query_arg($term,$queryString);
$output .= sprintf("<a class=\"$filterclass\" title=\"Remove filter: $term_name\" href=\"$blogurl/?%s\">%s</a>", $filterclear, $term_name);
} // END TAG_COUNT
} // END FILTER_COUNT
} // END FOR EACH
return $output;
}
?>