WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a Wordpress conditional that I can use to check for gallery images/media in a post?

I do not want to check if the [gallery] shortcode exist in a post.

Attached is a screenshot of what I want to check for and if is images I want to output them to the page.

Gallery has 4 image

share|improve this question
up vote 2 down vote accepted

No Need for SQL queries in the template.

function wpse_72594_get_attachments( $id, $mime = '' )
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' => $mime,
        'post_parent' => $id
    $attachments = get_posts($args);
    if ($attachments) 
        return $attachments;

    return false;

Then call the function like this (300 is the post ID):

  • wpse_72594_get_attachments(300), grabs all attachments
  • wpse_72594_get_attachments(300, 'image' ), only images
  • wpse_72594_get_attachments(300, 'application/pdf' ), only pdf files
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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