• Hello!

    I want to add pagination with get_posts() function.

    This is my code:

    $args = array(
       	 	'post_type' => 'attachment',
       	 	'post_status' => 'any',
       	 	'orderby' => 'rand', // select image by random.
       	 	'posts_per_page' => 1, // number of random image, default is 1 random image, you can change number to for example 10 to display 10 random images.
       	 	'post_mime_type' => array('image/png', 'image/x-png', 'image/jpeg', 'image/jpg', 'image/gif', 'image/bmp') // image mime type (image format), you can remove some mime type or adding custom mime type.
    	);
    
    	$images = get_posts($args);
    
    	foreach ($images as $image) {
    		$image_link = wp_get_attachment_url($image->ID); // random image link.
    		$image_title = $image->post_title; // random image title, you can remove it, not important.
    		$image_caption = $image->post_excerpt; // random image caption, you can remove it, not important.
    		echo '<p><img src="'.$image_link.'" title="'.$image_title.'" alt="'.$image_caption.'"></p>'; // display random image
    	}
    
    	wp_reset_postdata();
  • The topic ‘How to add pagination with get_posts function?’ is closed to new replies.