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 usually have a gallery in posts. I want to display random image and its post-title from a random published post which can lead to permalink like related posts below a post.

I used this code:

<?php
$args = array(
    'post_type' => 'attachment',
    'numberposts' => 3,
    'post_status' => null,
    'post_parent' => get_the_ID(),
    'post_mime_type' => 'image',
    'orderby' => 'rand'
); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( 'the_title' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );
    }
}

It displays image attachments which are not published from media library. And shows url as ../wp-content/uploads/2013/01/attachment-name not as abc.com/post-name/attachment-name.

Please suggest!!

Thanks.

share|improve this question
    
'post_status' => 'published' ? you are using null by the way... –  Reigel Jan 17 '13 at 11:25
    
tried 'publish' not working!! –  10wtaylor Jan 17 '13 at 11:28
    
Just to be clear, you want this: below your post you want to have three links to different posts, and each link should have a picture of the gallery of the linked post to it? –  fischi Jan 17 '13 at 12:04
    
Yes, fishci. 3 links to different random posts with their title but random gallery image as thumbnail pointing to post-permalink!! –  10wtaylor Jan 17 '13 at 12:11

1 Answer 1

up vote 1 down vote accepted

The solution to this is that you have to search for posts first, and afterwards to images to the post.

In my code I included a parameter exclude to ensure the current post is not delivered again.

Please keep in mind that if a post has no Image, none will be shown. Also, I did not include stylings or fallbacks, if there are no posts to show or something like that - but that should be no problem to work out.

Have fun!

$args = array(
    'post_type' => 'post',
    'numberposts' => 3,
    'post_status' => 'publish',
    'orderby' => 'rand',
    'exclude' => get_the_ID()
); 
$relatedposts = get_posts( $args );

echo '<ul>';

foreach ( $relatedposts as $related ) {

    $args = array(
        'post_type' => 'attachment',
        'numberposts' => 1,
        'post_parent' => $related->ID,
        'post_mime_type' => 'image',
        'orderby' => 'rand'
    ); 
    $attachments = get_posts($args);
    $thisimage = $attachments[0];

    echo '<li>';
        echo wp_get_attachment_image( $thisimage->ID, 'thumbnail' );
        echo '<a href="' . get_permalink( $related->ID ) . '">' . $related->post_title . '</a>';
    echo '</li>';

}

echo '</ul>';
share|improve this answer
    
wow, it works wonderfully, thanks fischi!! I have included all fallbacks. No problem. –  10wtaylor Jan 17 '13 at 19:49

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.