2010-07-14 3 views
2

je le code suivant dans mon functions.php:Wordpress: obtenir des pièces jointes, mais pas après le pouce

function get_images($size = 'thumbnail') { 

global $post; 
return get_children(array('post_parent' => get_the_ID(), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')); 

} 

Et dans mon single.php

<?php $photos = get_images('full'); ?> 

      <?php $x = 0; ?> 
      <?php foreach($photos as $photo): ?> 
       <?php if($x < 1): ?> 
        <img src="<?=wp_get_attachment_url($photo->ID)?>" alt="fullImg" /> 
       <?php endif; ?> 
       <?php $x++; 
       ?> 
      <?php endforeach; ?> 

Je veux pour montrer juste UNE image là, mais il me montre aussi l'image post-thumb , que je ne veux pas, y at-il une option pour exclure cela?

Répondre

7

C'est un codage fou ici! Il est inutile d'accepter une taille dans votre fonction, car elle renvoie simplement un tableau d'objets post. Une fois que vous avez vos messages, puis, utilisez les fonctions de pièce jointe appropriées pour obtenir les informations sur la taille de pièce jointe que vous recherchez.

Je proposerais une fonction comme ceci à la place;

function get_images($overrides = '', $exclude_thumbnail = false) 
{ 
    return get_posts(wp_parse_args($overrides, array(
     'numberposts' => -1, 
     'post_parent' => get_the_ID(), 
     'post_type' => 'attachment', 
     'post_mime_type' => 'image', 
     'order' => 'ASC', 
     'exclude' => $exclude_thumbnail ? array(get_post_thumbnail_id()) : array(), 
     'orderby' => 'menu_order ID' 
    ))); 
} 

Et de le mettre en pratique;

<?php if ($photo = get_images('numberposts=1', true)): ?> 

    <img src="<?php echo wp_get_attachment_url($photo[0]->ID); ?>" alt="fullimg" /> 

<?php endif; ?> 

MISE À JOUR: Typo dans la fonction - fixe.

Questions connexes