web-dev-qa-db-fra.com

Comment ajouter une image aléatoire à un article de la galerie et n'en afficher qu'une?

Je sais comment ajouter une image et une galerie. J'ai besoin d'avoir une image de la galère sur la page au hasard chaque fois que la page est chargée.

La page ne doit afficher qu'une image à la fois.

Existe-t-il un plugin ou un code court pour le faire? Je sais comment faire des galeries au hasard, mais elles montrent toutes les images.

Réponse :

$args = array( 
                'post_type' => 'attachment',
                'numberposts' => 1,
                'orderby' => 'Rand',
                'post_status' => null,
                'post_parent' => get_the_ID(),
                'post_mime_type'  => 'image'
            ); 
            have_posts(); //must be in the loop
            the_post(); //set the ID

            $images = get_children( $args );            

            if ($images) {
            foreach ( $images as $attachment_id => $attachment ) {
                    echo wp_get_attachment_image( $attachment_id, 'full' );
                }
            }
            wp_reset_query();
1
Kevin

Utilisez le paramètre 'orderby' => 'Rand' pour la fonction get_children() .

Par exemple:

$images = get_children( array(
    'orderby'        => 'Rand',       // this is random param
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'post_parent'    => get_the_ID(),
);
3
Wyck

Vous pouvez également extraire les identifiants de TOUSgaleries sur une page à l'aide de get_post_galleries() et vous n'avez pas besoin d'une boucle supplémentaire.

// pull all the images from all galleries as unique IDs
$images = array_unique( explode( ",", implode( ",", wp_list_pluck( get_post_galleries( get_the_ID(), false ), 'ids' ) ) ) );

// randomize the order
shuffle( $images );

// pull the first id
list ( $id ) = $images;

// convert to image
echo wp_get_attachment_image( $id, 'full' );

Référence

1
jgraup

Ce code répond à la question. Dans une page ou un article contenant une galerie, une image aléatoire de la galerie sera affichée et seule cette image sera affichée. Il entre dans la boucle wordpress (le code ici inclut en fait la boucle).

<?php //start the loop ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

            <?php // get the post's gallery ?>
            <?php if ( get_post_gallery() ) : $gallery = get_post_gallery( get_the_ID(), false ); ?>

                <?php //get gallery picture ids string seperates the ids and puts them in an array. ?>
                <?php $pic_ids = explode(",", $gallery['ids']);?>

                <?php // set a random int < to the size of the array containing ids.?> 
                <?php $i=Rand(0, count($pic_ids)-1);?>

                    <?php //get image corresponding to the random id?>
                    <?php echo wp_get_attachment_image( $pic_ids[$i],'full', false, '' ); ?>
                <?php endif; ?>
        <?php endwhile; else : ?>

            <p><?php _e( 'Sorry, no page found.' ); ?></p>

        <?php endif; ?>
0