Skip to content

Instantly share code, notes, and snippets.

@kaweski
Created November 17, 2016 17:35
Show Gist options
  • Save kaweski/21ee56b46b5eb5f5f4de09bcbee4d3c4 to your computer and use it in GitHub Desktop.
Save kaweski/21ee56b46b5eb5f5f4de09bcbee4d3c4 to your computer and use it in GitHub Desktop.
Lista as subcategorias de uma categoria num select.
<form action="#" class="form-field" id="ID_DO_SELECT">
<?php
$taxonomia = 'ID_DA_SUA_TAXONOMIA';
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => $taxonomia,
'hide_empty' => 0,
);
$categories = get_categories( $args );
foreach ($categories as $category) {
echo '<div class="form-field-select"><select name="NOME_DO_CAMPO_AQUI" id="NOME_DO_CAMPO_AQUI-'.$category->cat_ID.'"><option>'.$category->cat_name.'</option>';
$theid = $category->term_id;
$children = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$theid" );
$no_children = count($children);
// VERIFICA SE A CATEGORIA POSSUI SUBCATEGORIA
if ($no_children > 0) {
$args2 = array(
'orderby' => 'name',
'parent' => 1,
'taxonomy' => $taxonomia,
'hide_empty' => 0,
);
$args2["parent"] = $category->term_id;
$categories2 = get_categories( $args2 );
foreach ($categories2 as $category2)
echo '<option value="'.get_category_link( $category2->cat_ID ).'">'.$category2->cat_name.'</option>';
}
echo '</select><!--/.NOME_DO_CAMPO_AQUI--></div><!--/.form-field-select-->';
} ?>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment