Aggiungere tag a un custom post type

9 set 2014, 14:45:42
Visualizzazioni: 16.4K
Voti: 1

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.

2
Commenti

Qual è la funzione di questi theme_locals

Pieter Goosen Pieter Goosen
9 set 2014 15:40:50

@PieterGoosen Onestamente non ne ho idea. Devono far parte del framework (cherry framework).

Phill Healey Phill Healey
9 set 2014 16:35:02
Tutte le risposte alla domanda 6
0

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
    )
);
9 set 2014 16:33:44
4
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);
}
9 set 2014 14:59:26
Commenti

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.

Phill Healey Phill Healey
9 set 2014 15:26:05

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

Pieter Goosen Pieter Goosen
9 set 2014 15:42:16

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

Tomás Cot Tomás Cot
9 set 2014 15:57:54

@PieterGoosen Ok, utilizza gli stessi tag di un altro custom post type allora.

Phill Healey Phill Healey
9 set 2014 16:18:24
2

prova questo

register_taxonomy(
        'team_tag', 
        'team', 
        array( 
            'hierarchical'  => false, 
            'label'         => __( 'Tag', CURRENT_THEME ), 
            'singular_name' => __( 'Tag', CURRENT_THEME ), 
            'rewrite'       => true, 
            'query_var'     => true 
        )  
    );
1 dic 2015 09:01:40
Commenti

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

Pieter Goosen Pieter Goosen
1 dic 2015 10:49:47

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.

MrMesees MrMesees
13 giu 2017 15:03:07
0

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

6 mar 2018 20:22:17
0

Mi mancava anche la casella dei tag nella schermata di modifica CPT dell'amministrazione. È apparsa aggiungendo 'show_in_rest' => true, nei parametri di register_taxonomy().

29 lug 2021 14:35:31
0

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;
    }
15 set 2023 12:55:41