Skip to content

Instantly share code, notes, and snippets.

@annelyse
Created July 12, 2021 13:14
Show Gist options
  • Save annelyse/2d4a469be80d966b4ca896bdefee7e7d to your computer and use it in GitHub Desktop.
Save annelyse/2d4a469be80d966b4ca896bdefee7e7d to your computer and use it in GitHub Desktop.
<?php
function terms_list($taxonomy, $orderby, $order, $empty = true, $type = "radio")
{
$term_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $empty,
);
$terms = get_terms($taxonomy, $term_args);
// var_dump( get_taxonomy($taxonomy));
$label = get_taxonomy($taxonomy);
if( is_tax('tax_category')){
$queryObject= get_queried_object();
$queryObjectID = $queryObject->term_id;
}
if ($type == 'radio') {
if ($terms) {
foreach ($terms as $term) {
$color = get_field('cat_color', 'term_' . $term->term_id);
$style = "";
if (!empty($color)) {
$style = 'style="color:' . $color . ';"';
};
$checked='';
if( !empty( $queryObjectID ) && $queryObjectID == $term->term_id ){
$checked = 'checked';
}
echo '<div class="form-group" ' . $style . '>';
echo '<input type="radio" '.$checked.' name="'. $taxonomy.'" value="' . $term->term_id . '" id="filter-' . $term->slug . '">';
echo '<label for="filter-' . $term->slug . '"><span>' . $term->name . '</span></label>';
echo '</div>';
}
}
}
if ($type == 'select') { ?>
<select name="<?= $taxonomy; ?>" id="<?= $taxonomy; ?>">
<option value=""><?= $label->label; ?></label>
<?php
if ($terms) {
foreach ($terms as $term) {
$color = get_field('cat_color', 'term_' . $term->term_id);
$style = "";
if (!empty($color)) {
$style = 'style="color:' . $color . ';"';
};
echo '<option ' . $style . ' value=' . $term->term_id . '"><span>' . $term->name . '</span></label>';
}
}
?>
</select>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment