WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pull random attachment thumbnail from specific category (14 posts)

  1. shaneholden
    Member
    Posted 5 years ago #

    Could someone help me with the code that would randomly pull a thumbnail from a specified post category?

    I've been trying the code in http://wordpress.org/support/topic/140609?replies=49 as well as trying to combine some to use the category, but I'm not getting it to work.

    Basically wanting to do what Matt has on his page that he calls Serendipity.

    Any help would be greatly appreciated!

  2. mores
    Member
    Posted 5 years ago #

    you want to use 'orderby' => 'rand()' for that.

    if ( $images = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'numberposts' => 1,
    		'orderby' => 'rand()',
    		'post_mime_type' => 'image',))){

    There's a problem with this method that actually caused me to remove this feature from the site I was working on. The client didn't mind since it was already graphics-intensive. But I didn't get this thing to work properly so I suggest you look for a plugin.

  3. shaneholden
    Member
    Posted 5 years ago #

    Mores,

    Thanks for the reply!

    How do you set which category would be used in that code? Could you post the full code that you had working?

    Thanks again

  4. mores
    Member
    Posted 5 years ago #

    Here's my entire random image function that I used in the functions.php.

    function randomimage_mores($size=medium,$num=1,$cat=0) {
    	$posts=query_posts('cat='.$cat);
    	foreach( $posts as $post ) {
    		if ( $images = get_children(array(
    			'post_parent' => $post->ID,
    			'post_type' => 'attachment',
    			'numberposts' => $num,
    			'orderby' => 'rand()',
    			'post_mime_type' => 'image',)))
    		{
    			$zeiger = 1;
    
    			foreach( $images as $image ) {
    				if ($zeiger != $num) {
    					$zwischen="    ";
    				} else {
    					$zwischen = "";
    					$zeiger = 1;
    				}
    				$attachmenturl=wp_get_attachment_url($image->ID);
    				$attachmentimage=wp_get_attachment_image( $image->ID, $size );
    				echo '<a href="'.$attachmenturl.'" rel="lightbox">'.$attachmentimage.'</a>'.$zwischen;
    				$zeiger++;
    			}
    		} else {
    			echo "No Image";
    		}
    	}
    }

    But, like I said, it didn't behave like I wanted it to, so I dropped it. Maybe it'll work for you, though.

  5. shaneholden
    Member
    Posted 5 years ago #

    Thanks Mores!

    Now it pulls images from the set cat. I've just got to figure out how to limit it to 1 attachment to show rather than the random 10 that it's doing.

  6. mores
    Member
    Posted 5 years ago #

    Use the $zeiger variable. I used it insert spaces between the images but the last image in a row does not get the added spaces.
    Like, if there are 10 images, you only add a bump after the first nine ...

    You can use it to quit after the desired number of images.

  7. shaneholden
    Member
    Posted 5 years ago #

    Thanks again for the reply!

    I have got to be making this harder than it should be. No matter what I try for the $zeiger variable, I either get 10 images showing, or 1k+. I don't see what is defining for it to originally pull 10 images rather than the number set for $num.

    Thank you again

  8. mores
    Member
    Posted 5 years ago #

    Hmmm ... maybe the $posts=query_posts('cat='.$cat); thing pulls the maximum number of posts, which are defined in your WP settings.
    Try adding using $posts=query_posts('showposts=1&cat='.$cat);

  9. shaneholden
    Member
    Posted 5 years ago #

    Perfect! Thank you soo much Mores!

    Best Regards!
    Shane

  10. shaneholden
    Member
    Posted 5 years ago #

    Well it seems to be showing just fine. My only problem now is it pulls attachments even if they are not in the set category. If you happen to know another way to set a category, I'd very much appreciate it. If not, I'll keep playing w/ it and searching, but for now it's much much closer and will work :)

    <?php function randomimage($size=medium,$num=1,$cat=0) {
    	$posts=query_posts('showposts=1&cat='.$cat);
    	foreach( $posts as $post ) {
    		if ( $images = get_children(array(
    			'post_type' => 'attachment',
    			'numberposts' => $num,
    			'orderby' => 'rand()',
    			'post_mime_type' => 'image',)))
    		{
    			$zeiger = 1;
    
    			foreach( $images as $image ) {
    				if ($zeiger != $num) {
    					$zwischen="&nbsp;&nbsp;&nbsp;&nbsp;";
    				} else {
    					$zwischen = "";
    					$zeiger = 1;
    				}
    				$attachmenturl=get_permalink($image->ID);
    				$attachmentimage=wp_get_attachment_image( $image->ID, $size );
    				echo '<a href="'.$attachmenturl.'">'.$attachmentimage.'</a>'.$zwischen;
    				$zeiger++;
    			}
    		} else {
    			echo "No Image";
    		}
    	}
    }
    ?>
  11. shaneholden
    Member
    Posted 5 years ago #

    One quick bump, hoping mores or someone else could help me fix the category issue. It's not limiting the category to what I choose.

    Thanks

  12. mores
    Member
    Posted 5 years ago #

    Do you call the function properly?

    <?php randomimage('thumbnail','1','6'); ?>

    (6 is just an example!)

  13. shaneholden
    Member
    Posted 5 years ago #

    Yessir, exactly like your example.

    Thank you again!

  14. shaneholden
    Member
    Posted 5 years ago #

    Anyone have any other ideas I could try?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.