Aggiungere tag a un custom post type
Sto utilizzando il framework Cherry su un sito WordPress. Include un custom post type che può essere usato per aggiungere 'Team Members' e creare pagine dello staff ecc.
Ho bisogno di espanderlo per poter aggiungere tag a ogni 'membro del team' in modo da poterli etichettare come appartenenti a un dipartimento a / b / c / ecc.
Il custom post type è registrato nel file theme-init.php usando questo codice:
/* Our Team */
function my_post_type_team() {
register_post_type( 'team',
array(
'label' => theme_locals("our_team"),
'singular_label' => theme_locals("our_team"),
'_builtin' => false,
// 'exclude_from_search' => true, // Escludi dai risultati di ricerca
'capability_type' => 'page',
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'menu_position' => 5,
'menu_icon' => ( version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ) ? 'dashicons-businessman' : '',
'rewrite' => array(
'slug' => 'team-view',
'with_front' => FALSE,
),
'supports' => array(
'title',
'editor',
'thumbnail',
)
)
);
}
add_action('init', 'my_post_type_team');
Voglio aggiungere i tag a questo post type, in modo che quando aggiungo un nuovo membro del team, possa anche assegnarlo a un dipartimento attraverso l'aggiunta di un tag rilevante. Attualmente la casella di modifica dei tag non appare nella pagina di aggiunta/modifica.
Quindi, ho modificato il codice sopra per includere una register_taxonomy in questo modo:
/* Our Team */
function my_post_type_team() {
register_post_type( 'team',
array(
'label' => theme_locals("our_team"),
'singular_label' => theme_locals("our_team"),
'_builtin' => false,
// 'exclude_from_search' => true, // Escludi dai risultati di ricerca
'capability_type' => 'page',
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'menu_position' => 5,
'menu_icon' => ( version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ) ? 'dashicons-businessman' : '',
'rewrite' => array(
'slug' => 'team-view',
'with_front' => FALSE,
),
'supports' => array(
'title',
'editor',
'thumbnail',
)
)
);
register_taxonomy(
'team_tag',
'team',
array(
'hierarchical' => false,
'label' => theme_locals("tags"),
'singular_name' => theme_locals("tag"),
'rewrite' => true,
'query_var' => true
)
);
}
add_action('init', 'my_post_type_team');
Tuttavia, non vedo ancora la casella dei tag apparire nella pagina di modifica nell'area di amministrazione.
Ogni aiuto su questo sarà molto apprezzato.

Sembra che il problema fosse in parte dovuto alla presenza di un file theme-init.php nel tema figlio che sovrascriveva parti del file theme-init.php nel tema genitore / framework Cherry.
Ho risolto il problema aggiungendo il seguente codice nel file theme-init.php del mio tema figlio:
register_taxonomy('team_tag', 'team', array(
'hierarchical' => false,
'label' => theme_locals("tags"),
'singular_name' => theme_locals("tag"),
'rewrite' => true,
'query_var' => true
)
);

register_post_type( 'team',
array(
'label' => theme_locals("our_team"),
'singular_label' => theme_locals("our_team"),
'_builtin' => false,
// 'exclude_from_search' => true, // Escludi dai risultati di ricerca
'capability_type' => 'page',
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'menu_position' => 5,
'menu_icon' => ( version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ) ? 'dashicons-businessman' : '',
'rewrite' => array(
'slug' => 'team-view',
'with_front' => FALSE,
),
'supports' => array(
'title',
'editor',
'thumbnail',
),
'taxonomies' => array('team_tag')
)
);
Come puoi vedere, ho aggiunto il parametro taxonomies
alla funzione register_post_type
.
Anche se questo codice dovrebbe funzionare, potresti provare a creare la relazione successivamente, con questo:
add_action('init', 'add_tax_post_rel');
function add_tax_post_rel() {
register_taxonomy_for_object_type('team_tag', 'team', 11);
}

Grazie per la risposta, ma non funziona. Se cambio 'team_tag' in 'tag' e poi aggiungo 'tags' all'array di supporto, ottengo la casella dei tag nel sottomenu e nella pagina dell'editor. Tuttavia questo utilizza i tag globali del sito.

@PhillHealey non può essere, i tag integrati sono sotto la tassonomia post_tag
, non tag

@PhillHealey, ho aggiunto un metodo alternativo per creare la relazione.

Per favore aggiungi risposte adeguate, cioè se aggiungi del codice, spiega cosa fa il tuo codice e come funziona. Grazie

Questo contiene codice quasi identico senza una definizione di funzione opaca a cui gli utenti non hanno accesso. In molti modi questo dovrebbe essere il codice utilizzato nella risposta accettata poiché si applica a tutti i WP, non solo al framework cherry se non c'è un commento al codice o una definizione di funzione. Ha persino suscitato una domanda da parte tua.

Puoi provare questo
add_action( 'init', 'create_client_tax' );
function create_client_tax() {
register_taxonomy(
'client_tag', //tassonomia dei tag
'client', // Il tuo post type
array(
'hierarchical' => false,
'label' => __( 'Tag', CURRENT_THEME ),
'singular_name' => __( 'Tag', CURRENT_THEME ),
'rewrite' => true,
'query_var' => true
)
);
}
Forse questo ti sarà d'aiuto

Se hai bisogno di visualizzare i tag per un custom post type - ecco un buon esempio https://jamper.online/vyvod-tegov-tags-dlya-kastomnyh-postov-custom-post-type
/**
* Ottiene i termini di una tassonomia specifica per un custom post type
* @param string $post_type Il nome del custom post type
* @param string $taxonomy Il nome della tassonomia
* @return array Array di oggetti termine
*/
function get_terms_by_custom_post_type( $post_type, $taxonomy ){
$args = array( 'post_type' => $post_type );
$loop = new WP_Query( $args );
$postids = array();
// costruisce un array di ID dei post
while ( $loop->have_posts() ) : $loop->the_post();
array_push($postids, get_the_ID());
endwhile;
// ottiene i valori della tassonomia basati sull'array di ID
$taxonomies = wp_get_object_terms( $postids, $taxonomy );
wp_reset_postdata();
return $taxonomies;
}
