Ajout dans le fichier functions.php :
// GESTION DE DOCUMENTS DANS LES PAGES
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
$formats = array('xls', 'pdf', 'doc', 'docx', 'csv', 'mp3', 'odt');
if(in_array(ext($attachment->guid), $formats))
{
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
}
function getSize($file){
$bytes = filesize($file);
$s = array('b', 'Kb', 'Mb', 'Gb');
$e = floor(log($bytes)/log(1024));
return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
}
function ext($file){
$file = substr($file,strlen($file)-5);
$file = substr($file,strpos($file,".")+1);
return $file;
}
function img($ext){
switch ($ext) {
case 'xls':
return "/img/type_files/xls.png";
break;
case 'mp3':
return "/img/type_files/mp3.png";
break;
case 'odt':
return "/img/type_files/odt.png";
break;
case 'pdf':
return "/img/type_files/pdf.png";
break;
case 'doc':
return "/img/type_files/doc.png";
break;
case 'docx':
return "/img/type_files/doc.png";
break;
case 'csv':
return "/img/type_files/csv.png";
break;
default:
return "/img/type_files/_blank.png";
break;
}
}
Vous pouvez choisir les différents type de fichiers autorisés dans le tableau $formats.
Ajout du répertoire d’image dans le thème : type_files.zip
Puis ajout dans un template de page de la boucle qui liste les documents :
<div class="base_docu">
<?php
$listDoc = "";
$listDoc = array();
$i=0;
$args = array( 'post_type' => 'attachment', 'numberposts' => 123456789, 'post_parent' => get_the_ID());
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $att ) {
$a = wp_get_attachment($att->ID);
if($a){
$listDoc[$i]['id'] = $att->ID;
$listDoc[$i]['title'] = $a['title'];
$listDoc[$i]['alt'] = $a['alt'];
$listDoc[$i]['caption'] = $a['caption'];
$listDoc[$i]['description'] = $a['description'];
$listDoc[$i]['href'] = wp_get_attachment_url($att->ID);
$listDoc[$i]['src'] = $a['src'];
$listDoc[$i]['date'] = $date;
$listDoc[$i]['size'] = "size";
$listDoc[$i]['ext'] = ext($listDoc[$i]['src']);
$listDoc[$i]['img'] = img($listDoc[$i]['ext']);
$i++;
}
}
}
?>
<?php for ($k=0;$k<count($listDoc);$k++) { ?>
<div class="un_doc">
<a href="<?php echo $listDoc[$k]['href']; ?>" title="<?php echo ucfirst($listDoc[$k]['title']); ?>" rel="attachment" target="_blank">
<span class="img">
<img src="<?php bloginfo("stylesheet_directory"); ?><?php echo $listDoc[$k]['img']; ?>" />
</span>
<span class="txt">
<?php echo ucfirst($listDoc[$k]['title']); ?>
<?php echo strtoupper($listDoc[$k]['ext']); ?>
<?php echo $listDoc[$k]['description']; ?>
</span>
</a>
</div>
<?php } ?>
</div>
Ajouter votre avis