WordPress hwk Blog ACF: Créer une Taxonomie pour Catégoriser ses Groupes de Champs

ACF: Créer une Taxonomie pour Catégoriser ses Groupes de Champs

8 April 2018

Enregistrer la Taxonomie

<?php
add_action('init', 'hwk_acf_taxonomy_register');
function hwk_acf_taxonomy_register(){
register_taxonomy('acf-field-group-type', array('acf-field-group'), array(
'labels' => array(
'name' => _x( 'ACF Field Groups Types', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Type', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Types', 'textdomain' ),
'all_items' => __( 'All Types', 'textdomain' ),
'parent_item' => __( 'Parent Type', 'textdomain' ),
'parent_item_colon' => __( 'Parent Type:', 'textdomain' ),
'edit_item' => __( 'Edit Type', 'textdomain' ),
'update_item' => __( 'Update Type', 'textdomain' ),
'add_new_item' => __( 'Add New Type', 'textdomain' ),
'new_item_name' => __( 'New Type Name', 'textdomain' ),
'menu_name' => __( 'Type', 'textdomain' ),
),
'hierarchical' => true,
'public' => false,
'show_ui' => true,
'show_admin_column' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
));
}

Fonctions additionnelles

<?php
add_action('admin_menu', 'hwk_acf_taxonomy_submenu');
function hwk_acf_taxonomy_submenu(){
if(!acf_get_setting('show_admin'))
return;
$cap = acf_get_setting('capability');
add_submenu_page('edit.php?post_type=acf-field-group', 'Types', 'Types', $cap, 'edit-tags.php?taxonomy=acf-field-group-type');
}
add_filter('parent_file', 'hwk_acf_taxonomy_submenu_highlight');
function hwk_acf_taxonomy_submenu_highlight($parent_file){
global $submenu_file, $current_screen, $pagenow;
if($current_screen->taxonomy == 'acf-field-group-type' && ($pagenow == 'edit-tags.php' || $pagenow == 'term.php'))
$parent_file = 'edit.php?post_type=acf-field-group';
return $parent_file;
}
add_filter('manage_edit-acf-field-group_columns', 'hwk_acf_taxonomy_column', 11);
function hwk_acf_taxonomy_column($columns){
$new = array();
foreach($columns as $key => $value) {
if($key == 'title')
$new['type'] = 'Type';
$new[$key] = $value;
}
return $new;
}
add_action('manage_acf-field-group_posts_custom_column' , 'hwk_acf_taxonomy_column_html', 10, 2 );
function hwk_acf_taxonomy_column_html($column, $post_id){
if($column == 'type'){
if(!$terms = get_the_terms($post_id, 'acf-field-group-type'))
return;
$final = array();
foreach($terms as $term){
$final[] = '<a href="' . admin_url('edit.php?acf-field-group-type='.$term->slug.'&post_type=acf-field-group') . '">' . $term->name . '</a>';
}
echo implode(', ', $final);
}
}
add_filter('views_edit-acf-field-group', 'hwk_acf_taxonomy_column_views', 9);
function hwk_acf_taxonomy_column_views($views){
if(!$terms = get_terms('acf-field-group-type', array('hide_empty' => false)))
return $views;
foreach($terms as $term){
$count = '';
if($term->count > 0)
$count = '<span class="count">(' . $term->count . ')</span>';
$views['type-' . $term->slug] = '<a href="' . admin_url('edit.php?acf-field-group-type=' . $term->slug . '&post_type=acf-field-group') . '">' . $term->name . '</a>' . $count;
}
return $views;
}

Style CSS

.column-type{
width:125px;
}
td.column-type a{
word-wrap: break-word;
padding: 2px 5px;
margin: 0 1px;
border-radius:2px;
background: rgba(0,0,0,.05);
color:#333;
}


Konrad Chmielewski

Evangeliste WordPress & Full Stack Developer depuis 10 années.