Last active
December 26, 2015 22:38
-
-
Save leobaiano/7224070 to your computer and use it in GitHub Desktop.
Exibir o filtro por taxonomy na listagem de posts de CPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' ); | |
function my_restrict_manage_posts() { | |
$CPT = "SEU_CPT"; | |
$slug_taxonamy = "SLUG_DA_TAXONOMY"; | |
global $typenow; | |
if ($typenow == $CPT) { | |
$filters = array($slug_taxonamy); | |
foreach ($filters as $tax_slug) { | |
// retrieve the taxonomy object | |
$tax_obj = get_taxonomy($tax_slug); | |
$tax_name = $tax_obj->labels->name; | |
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>"; | |
echo "<option value=''>Show All $tax_name</option>"; | |
generate_taxonomy_options($tax_slug,0,0); | |
echo "</select>"; | |
} | |
} | |
} | |
function generate_taxonomy_options($tax_slug, $parent = '', $level = 0) { | |
$args = array('show_empty' => 1); | |
if(!is_null($parent)) { | |
$args = array('parent' => $parent); | |
} | |
$terms = get_terms($tax_slug,$args); | |
$tab=''; | |
for($i=0;$i<$level;$i++){ | |
$tab.='--'; | |
} | |
foreach ($terms as $term) { | |
echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . $term->count .')</option>'; | |
generate_taxonomy_options($tax_slug, $term->term_id, $level+1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment