Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this page where I am showing all post's attachments.

This is the code:

<?php
$args = array(
    'post_type' => 'attachment',
    'post_status' => 'published',
    'posts_per_page' =>25,
    'post_parent' => null, // Post-> ID;
    'numberposts' => null,
);

$attachments = get_posts($args);

$post_count = count ($attachments);

if ($attachments) {
    foreach ($attachments as $attachment) {
    echo "<div class=\"post photo col3\">";
        $url = get_attachment_link($attachment->ID);// extraigo la _posturl del attachmnet      
        $img = wp_get_attachment_url($attachment->ID);
        $title = get_the_title($attachment->post_parent);//extraigo titulo
        echo '<a href="'.$url.'"><img title="'.$title.'" src="'.$img.'"></a>';
        echo "</div>";
    }   
}
?>

Now, I have the 'post', 'page' and 'videos' post_types. I am getting all of them but I want to display 'post' post_type only. How do I exclude 'video' post_type?

Thanks!

share|improve this question

2 Answers 2

$posts = get_posts( array( 'post_type' => 'post', 'posts_per_page' => -1 ) );

     foreach ( $posts as $post ) {

         $attachments = get_children( array (
            'post_parent'    => $post->ID,
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'post_per_page'  => 25
        ), ARRAY_A );


      if( $attachments ) {
         //continue with what you were doing

      }
   }
share|improve this answer
    
Not working for me... It's giving me a sever error while it's working normally if I undo the change. –  user1341839 Aug 7 '12 at 13:10
    
You were probably missing the closing } from the foreach loop. I updated the sample code to include it. –  Chris_O Aug 7 '12 at 18:57

just paste the following code anywhere in your post file and attachments will be displayed.

$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        the_attachment_link($attachment->ID, false);
    }
}
share|improve this answer

Your Answer

 
discard

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.