Cách thêm cột quản trị viên Thời gian trong Thư viện phương tiện WordPress

Tìm cách hiển thị thời gian mà phương tiện đã được đính kèm / tải lên thư viện WordPress của bạn cùng với ngày tháng?

Thêm phần sau vào functions.php của chủ đề con của bạn :

add_filter( 'manage_media_columns', 'sk_media_columns_time' );
/**
 * Filter the Media list table columns to add a Time column.
 *
 * @param array $posts_columns Existing array of columns displayed in the Media list table.
 * @return array Amended array of columns to be displayed in the Media list table.
 */
function sk_media_columns_time( $posts_columns ) {
	$posts_columns['time'] = _x( 'Time', 'column name' );

	return $posts_columns;
}

add_action( 'manage_media_custom_column', 'sk_media_custom_column_time' );
/**
 * Display attachment uploaded time under `Time` custom column in the Media list table.
 *
 * @param string $column_name Name of the custom column.
 */
function sk_media_custom_column_time( $column_name ) {
	if ( 'time' !== $column_name ) {
		return;
	}

	the_time( 'g:i:s a' ); // in hh:mm:ss am/pm format.

}

add_action( 'admin_print_styles-upload.php', 'sk_time_column_css' );
/**
 * Add custom CSS on Media Library page in WP admin
 */
function sk_time_column_css() {
	echo '<style>
			.fixed .column-time {
				width: 10%;
			}
		</style>';
}

Thời gian được hiển thị sẽ nằm trong múi giờ được đặt trong cài đặt WordPress. Giá trị mặc định là UTC + 0.

Nếu bạn muốn hiển thị thời gian ở định dạng được đặt trong WordPress, hãy thay thế bằng .the_time( 'g:i:s a' ) the_time()

Ngoài ra, để làm cho cột Thời gian có thể sắp xếp, hãy thêm:

add_filter( 'manage_upload_sortable_columns', 'sk_sortable_time_column' );
/**
 * Register Time column as sortable in the Media list table.
 *
 * @param array $columns Existing array of columns.
 */
function sk_sortable_time_column( $columns ) {
	$columns['time'] = 'time';

	return $columns;
}