0

I have a CPT and on back-end archive list I need to add two columns and display element of two ACF field (checkbox field).

I try to use this code but nothing appear on back-end:

CPT:

 /* Palinsesto */ 
add_action('init', 'crea_palinsesto'); #[1] function
function crea_palinsesto() {
    $labels = array(
        'name'               => __( 'Palinsesto' ),
        'singular_name'      => __( 'Palinsesto' ),
        'add_new'            => __( 'Aggiungi Programma' ),
        'add_new_item'       => __( 'Nuovo Programma' ),
        'edit_item'          => __( 'Modifica Programma' ),
        'new_item'           => __( 'Nuovo Programma' ),
        'all_items'          => __( 'Elenco Programmi' ),
        'view_item'          => __( 'Visualizza Programma' ),
        'search_items'       => __( 'Cerca Programma' ),
        'not_found'          => __( 'Nessun Programma trovato' ),
        'not_found_in_trash' => __( 'Nessun Prorgamma trovato nel cestino' ),
    );
    $args   = array(
        'labels'        => $labels, # [2]
        'public'        => TRUE,    # [3]
        'rewrite'       => array( 'slug' => 'palinsesto' ), # [4]
        'has_archive'   => TRUE, # [5]
        'hierarchical'  => FALSE, # [6]
        'menu_position' => 7, # [7]
        'menu_icon'     => 'http://padrepiotv.hellonetlab.it/wp-content/uploads/palinsesto-icon-cpt.png',
        'supports'      => array( # [8]
            'title',
            'editor',
            'thumbnail'
        ),
    );
    register_post_type( 'palinsesto', $args ); #[9]
}

Code for columns

add_filter( 'manage_edit_palinsesto_columns', 'my_edit_palinsesto_columns' ) ;

function my_edit_palinsesto_columns( $columns ) {

    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => __( 'Programma' ),
        'duration' => __( 'Giorno' ),
        'genre' => __( 'Fascia Oraria' )
    );

    return $columns;
}

add_action( 'manage_palinsesto_posts_custom_column', 'my_manage_palinsesto_columns', 10, 2 );

function my_manage_palinsesto_columns( $column, $post_id ) {
    global $post;

    switch( $column ) {

        /* If displaying the 'duration' column. */
        case 'duration' :

            /* Get the post meta. */
            $duration = get_post_meta( $post_id, 'giorno_programmazione', true );

            /* If no duration is found, output a default message. */
            if ( empty( $duration ) )
                echo __( 'Unknown' );

            /* If there is a duration, append 'minutes' to the text string. */
            else
                printf( __( '%s minutes' ), $duration );

            break;

        /* If displaying the 'genre' column. */
        case 'genre' :

            /* Get the genres for the post. */
            $terms = get_the_terms( $post_id, 'genre' );

            /* If terms were found. */
            if ( !empty( $terms ) ) {

                $out = array();

                /* Loop through each term, linking to the 'edit posts' page for the specific term. */
                foreach ( $terms as $term ) {
                    $out[] = sprintf( '<a href="%s">%s</a>',
                        esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'genre' => $term->slug ), 'edit.php' ) ),
                        esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'genre', 'display' ) )
                    );
                }

                /* Join the terms, separating them with a comma. */
                echo join( ', ', $out );
            }

            /* If no terms were found, output a default message. */
            else {
                _e( 'No Genres' );
            }

            break;

        /* Just break out of the switch statement for everything else. */
        default :
            break;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.