WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_children + category? (another random image issue) (5 posts)

  1. mores
    Member
    Posted 6 years ago #

    Hi guys.
    I'm almost done, I can see the top of the mountain, but there is a string attached that won't let me make the final steps.

    I need X random images on my start page. Don't want plugins, needs to be in a functions.php file. It all works nice, even with the spacing between the images and no spacer after the last one etc.

    HOW DO I LIMIT THIS TO JUST ONE CATEGORY ?????
    Please, someone help me exchange the "get_children" part for a good SQL thingie, with the JOIN LEFT and whatnot, to get only attachments from a certain category. I would be really, really grateful.

    Here's my code:

    function randomimage_mores($size=medium,$num=1) {
    	if ( $images = get_children(array(
    		'post_type' => 'attachment',
    		'category' => 'Fotos',
    		'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";
    	}
    }

    You can see, I tried blindly using a "category" tag in the get_children, but to no avail. The "$zeiger" variable is there to see how many cycles I've done in the foreach loop, so that I do not add a spacer after the last image.

    Please ... help!

  2. omgitztrey
    Member
    Posted 6 years ago #

    Why can't you just add a
    WHERE clause?

    ex Where category = '1' or whatever

  3. omgitztrey
    Member
    Posted 6 years ago #

    Also, how would I be able to get the post url instead of the image url using the code you supplied?

  4. mores
    Member
    Posted 5 years ago #

    I can't use a "where category = X" because there is no category. It's a weird way, the categories are actually tags, and those are in some other table ... I must do somethign with left join or right join or something.

    img url / post url: no idea. I'd have to check that out in depth.

  5. mores
    Member
    Posted 5 years ago #

    Okay, gambit37 helped with the category problem, here's the solution:
    http://wordpress.org/support/topic/140609?replies=25#post-819263

    I then created a mix of his category image and my random image:

    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="&nbsp;&nbsp;&nbsp;&nbsp;";
    				} 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";
    		}
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic