Se inserite molte immagini o altri allegati nei post di WordPress potrebbe essere utile visualizzare il numero di allegati per ciascun post nelle colonne del backend di WordPress. Vediamo come fare.
Aggiungete il seguente codice al file functions.php
:
add_filter( 'manage_posts_columns', 'posts_columns_attachment_count', 5 );
add_action( 'manage_posts_custom_column', 'posts_custom_columns_attachment_count', 5, 2 );
function posts_columns_attachment_count( $defaults ) {
$defaults['wps_post_attachments'] = __( 'Allegati' );
return $defaults;
}
function posts_custom_columns_attachment_count( $column_name, $id ) {
if( $column_name === 'wps_post_attachments' ) {
$attachments = get_children( array( 'post_parent'=> $id ) );
$count = count( $attachments );
if( $count != 0 ) {
echo $count;
}
}
}