What I am trying to do is be able to find a random attachment in the category.php page belonging to one of the posts in that category and also be able to find a random attachment in the single.php belonging to that post.
What I have been able to get so far is to find a random attachment in a single post.
$images =& get_children( array( 'post_parent' => $post -> ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'rand', 'numberposts' => 1 ) ); foreach ( (array) $images as $attachment_id => $attachment ) : echo wp_get_attachment_url( $attachment_id ) . '<br>';</script>'; endforeach;
I tried adding 'cat' => 1, but it doesn't display anything.
I tried following the example http://codex.wordpress.org/Template_Tags/get_posts#Random_posts and output the titles to test it. I set post_type to attachment and it shows all the attachments in all categories. I set cat to 1 and it does not show anything (I know I have 1 attachment in 1 of the posts there). I delete post_type and it shows the post titles for cat 1.