将Alt Tag和Dimensions列添加到媒体库

时间:2021-12-09 作者:Konrad

我使用下面的代码在“媒体库”列中显示alt标记,但我还使用额外的列/代码来显示图像尺寸。现在唯一的问题是,尺寸标注也显示在alt tag列中。将标注数据添加到alt文本的代码中是什么?

希望有人能帮我,谢谢!

function wpse_media_extra_column( $cols ) {
    $cols["alt"] = "ALT";
    return $cols;
}
function wpse_media_extra_column_value( $column_name, $id ) {
    if( $column_name == \'alt\' )
        echo get_post_meta( $id, \'_wp_attachment_image_alt\', true);
}
add_filter( \'manage_media_columns\', \'wpse_media_extra_column\' );
add_action( \'manage_media_custom_column\', \'wpse_media_extra_column_value\', 10, 2 );

function wh_column( $cols ) {
    $cols["dimensions"] = "Dimensions (w, h)";
    return $cols;
}
function wh_value( $column_name, $id ) {
    $meta = wp_get_attachment_metadata($id);
           if(isset($meta[\'width\']))
           echo $meta[\'width\'].\' x \'.$meta[\'height\'];
}
add_filter( \'manage_media_columns\', \'wh_column\' );
add_action( \'manage_media_custom_column\', \'wh_value\', 10, 2 );

enter image description here

1 个回复
SO网友:DeltaG

无需每次添加新的管理列时都创建新函数。您可以使用以下功能在单独的管理列中显示两个媒体数据。

function column_id($columns) {
    $columns[\'media_alt\'] = __(\'Alt\');
    $columns[\'media_dimensions\'] = __(\'Dimensions\');
    return $columns;
} 
add_filter( \'manage_media_columns\', \'column_id\' );
 
function column_id_row($columnName, $columnID){
    if ( $columnName == \'media_alt\') {
        $image_alt = get_post_meta($columnID, \'_wp_attachment_image_alt\', true);
        echo $image_alt;
    }
    if ( $columnName == \'media_dimensions\') {
        $image_size = wp_get_attachment_image_src($columnID, \'full\');
        if ( $image_size ) {
            echo $image_size[1] . \' x \' . $image_size[2];
        }
    }
}
add_filter( \'manage_media_custom_column\', \'column_id_row\', 10, 2 );

相关推荐