How to slice an array using posts_per_page? [closed]

As I already mentioned in your previous question, setting the post per page will not help you. However, there is a workaround.

Instead of directly setting the post per page, use a variable and change that. Then use the same variable to slice the array. For example:

$per_page = 2;
// Use the $per_page value to set the posts per page
$args = array(
    'posts_per_page' => $per_page,
);
// The rest of your code here

    // Now, before returning the images, slice the array using
    // the variable we set before
    $images = array_slice($images, 0, $per_page);
    return $images;