Question

J'ai un get_images() de fonction dans laquelle je voudrais afficher toutes les pièces jointes d'image pour le poste actuel (ou la page) dans une liste avec la dernière image ayant un attribut class="last" pour marquer comme la dernière image dans la liste.

Le code ci-dessous est mon premier passage à obtenir les images jointes à l'affichage, cependant, sa seule liste une image de la boucle donc mon foreach est bâclé ...

function get_images() {
  global $post;
  $attachment = array_values(get_children(array( 
    'post_parent' => $post->ID, 
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'order' => 'ASC', 
    'numberposts' => 1 
  )));
  if ( $attachment ) {
    foreach($attachment as $attachmentImage) {
      echo '<img src="' . wp_get_attachment_url($attachmentImage->ID) . 
        '" class="post-attachment" />';
    }
  }
}
Était-ce utile?

La solution

Cela devrait le faire, je pense:

function get_images() {
  global $post;
  $attachment = get_children(array( 
      'post_parent' => $post->ID, 
      'post_type' => 'attachment', 
      'post_mime_type' => 'image', 
      'order' => 'ASC', 
      'numberposts'  => -1 ), 
    ARRAY_N );
  if ( $attachment ) {
    $attachment_count = count($attachment);
    foreach($i=0; $i < $attachment_count; $i++) {
      $last = ($i == ($attachment_count-1) ) ? ' last' : '';
      echo '<img src="' . wp_get_attachment_url($attachment[$i]->ID) . 
        '" class="post-attachment'.$last.'" />';
    }
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top