5

Need to do a check if post has attachments that are not images. I haven't seen this anywhere - thoughts?

6

You can use the following within a loop:

$files = get_attached_media( $type, $post_id );

Just define the attachment MIME type on $type. Second parameter is optional. Example from the Codex page:

$media = get_attached_media( 'audio', 102 );

With the retrieved array, you can do something like:

if( $media ) {
   //Show the post attachments that are not images, but audio
}
| improve this answer | |
  • hey thanks. I should specify, I'm looking for documents - .doc, .txt. .pdf, etc. – Jason Dec 15 '14 at 16:27
  • Check this: codex.wordpress.org/Uploading_Files, and upload your files. After uploading check the database, and to the posts table. Check the post_mime_type to see the exact MIME type of your file. And it can be any. :) – Mayeenul Islam Dec 15 '14 at 16:31
2

Here's how to check if the post has an attachment other than image (or other mime types):

        <?php $attachments = get_posts( array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_parent' => $post->ID,
        'exclude'     => 'image'
        ));


        if ( $attachments ) { ?>
               // do something
            <?php } ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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