0

I have installed this plugin (http://wordpress.org/plugins/media-categories-2/) to categorize my images.

In my functions.php I have put the following code to limit the number of images shown in the gallery:

function get_random_gallery_images(){
    global $wpdb,$post;
        $ids = "";
        $counter = 0;
        $number_of_posts = 1;
        $args = array(
        'post_type' => 'attachment',
        'numberposts' => 1,
        'post_status' => null,
        'orderby' => 'rand',
        'post_parent' => $post->ID
        );
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {

                if ($counter != 0) {
                    $ids .= ','.$attachment->ID;
                }
                else {
                    $ids .= $attachment->ID;
                }
                $counter++;
            }
        }
        return $ids;
} 

in my single.php I have put the following code:

$attachment_ids = get_random_gallery_images();
$category_current = get_the_category($post->ID) ; //for getting the images of that specific category
echo do_shortcode('[gallery columns="1" category="'.$category_current[0]->name.'" include="'.$attachment_ids.'" link="file"]');

The problem is that is does not matter if I change the values of numberposts. It always displays all of the images. What I am doing wrong? I only want to display 1 image.

1 Answer 1

0

Use posts_per_page instead of numberposts

So the $args for the get_posts would be like below and also the param for post_status is wrong. Check the codex for the details here

Also when when you are fetching one attachment it is useless to set a counter for it. You can use the modified function

function get_random_gallery_images(){
    global $wpdb, $post;

    $args = array(
    'post_type' => 'attachment',
    'posts_per_page' => 1,
    'post_status' => 'any',
    'orderby' => 'rand',
    'post_parent' => $post->ID
    );

    $attachments = get_posts( $args );

    if ($attachments) {
        foreach ( $attachments as $attachment ) {
            $ids = $attachment->ID;
        }
    }

    return $ids;
} 

For more details check the codex

11
  • I changed the $args code to post_per_page like you have mentioned, but it still displays all of the images. (removed the 'numberposts' => 1 )
    – Monique
    Commented Dec 18, 2013 at 11:04
  • can you debug and see what does the function get_random_gallery_images() returns, it should return one attachment id Commented Dec 18, 2013 at 11:11
  • I have debug on in my wp-config.php and I see no errors if fewing the post. (don't know how to to debug the specific function get_random_gallery_images()
    – Monique
    Commented Dec 18, 2013 at 11:20
  • you can user var_dump($attachment_ids) to see what it returns and why have you used the set of codes in index.php, Commented Dec 18, 2013 at 11:32
  • I am sorry. I cannot get it to work (I have very little knowledge of php) I don't know where to put the var_dump and how to read the results
    – Monique
    Commented Dec 18, 2013 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.