Ich entwickle eine Fotogalerie WP Site. Ich möchte “ul” Liste von Bildern hinzufügen, die zufällig aus der Galerie ausgewählt werden.
Wie würde ich zufällig eine Anzahl von Bildern aus der Mediathek auswählen?
Alle Hinweise würden sehr geschätzt werden.
Wenn Sie der Datei functions.php Ihres Themes Folgendes [wpse73055-random-images]
, können Sie einen Shortcode ( [wpse73055-random-images]
) mit einem optionalen num
Parameter verwenden (z. B. [wpse73055-random-images num=5]
, um 5 zufällige Bilder anzuzeigen).
function wpse73055_get_random_images( $atts ) { extract( shortcode_atts( array( 'num' => 10 ), $atts ) ); $args = array( 'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => $num, 'orderby' => 'rand' ); $images_query = new WP_Query( $args ); $images = $images_query->posts; $output = ''; foreach ( $images as $image ) { $output .= '- ' . wp_get_attachment_link( $image->ID ) . '
'; } $output .= '
'; return $output; } add_shortcode( 'wpse73055-random-images', 'wpse73055_get_random_images' );
wp_get_attachment_link
WP_Query
add_shortcode