• I’m having difficulty setting up a homepage gallery which feature images from a specific category of posts.

    Basically, what I want is a grid of thumbnails that show random images/attachments from a specific post category that when clicked on either link to the post that they’re in or a gallery of those attachments. Can this be done?

    The closest I have come to this is by using this code:

    <?php
    $post_parent = get_post($post->ID, ARRAY_A);
    $parent = $post_parent['post_parent'];
    
    $attachments = get_children( array( 'post_parent' => $post_id, 'posts_per_page' => '18', 'post_type' => 'attachment', 'orderby' => 'rand', 'order' => 'DESC') );
    foreach($attachments as $id => $attachment) :
            echo wp_get_attachment_link($id, 'thumbnail', false);
    endforeach;
    ?>

    This successfully pulled random images (but not from post categories) and when each thumb is clicked, they just open up the selected attachment on its own.

    Really struggling with this. Please help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Do you need pagination with this or do just want to show 18 random image attachments from posts from a specific category? I don’t think this can be done with a (one) query.

    Thread Starter ched53

    (@ched53)

    Really, I just wanted to show 18 random image attachments from posts from a specific category.

    Moderator keesiemeijer

    (@keesiemeijer)

    I changed the solution from this topic to show a random attachment from 18 posts (with an attachement) from a specific category: http://wordpress.org/support/topic/exclude-posts-without-attachments-from-query

    Try it with this:

    <?php
    	$category_ID = 25;
    
    	global $wpdb;
    
    	// get the first 18 posts with an attachment from the database
    	$posts = $wpdb->get_results("
    	  SELECT *
    	  FROM $wpdb->posts
    	  LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    	  LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    	  WHERE post_status = 'publish'
    	  AND ID IN (
    	    SELECT DISTINCT post_parent
    	    FROM $wpdb->posts
    	    WHERE post_parent > 0
    	    AND post_type = 'attachment'
    	    AND post_mime_type IN ('image/jpeg', 'image/png')
    	  )
    	  AND $wpdb->term_taxonomy.taxonomy = 'category'
    	  AND $wpdb->term_taxonomy.term_id = '" . $category_ID . "'
    	  ORDER BY RAND() LIMIT 0,18
    	");
    
    	if($posts){
    		foreach($posts as $post) {
    			setup_postdata($post);
    			$images = get_children(array(
    			  'post_parent' => $post->ID,
    			  'post_type' => 'attachment',
    			  'post_mime_type' => 'image',
    			  'orderby' => 'rand'
    			));
    
    			if(!empty($images)){
    				$ids = array_keys($images);
    				// link attachement to post
    				echo '<a href="' . get_permalink($post->ID) . '" >' . wp_get_attachment_image($ids[0], 'thumbnail') . '</a>';
    			}
    		}
    	}
    ?>

    Change $category_ID = 25; to the correct category ID;

    Thread Starter ched53

    (@ched53)

    wow that almost works!! Exactly what I had in mind. Only problem is, is it showing the post that is connected to the last image it displays:
    http://www.ched53.co.uk/home/

    Thanks for all your help so far

    Moderator keesiemeijer

    (@keesiemeijer)

    I’m not sure why that is happening. Try it with this: http://pastebin.com/CqHVXLic

    If that doesn’t help can you paste and submit the full code of the template file you’re using into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Thread Starter ched53

    (@ched53)

    That seems to work fine! Thank you so much. Exactly what I wanted 🙂

    I’ve selected my ‘featured’ category to be the one which images are pulled from to show off my work. However, now and then I will post a ‘behind the scenes’ style pic in these posts that I would rather not be pulled to the front page. Is there not a way of stopping certain images within this category from being pulled?

    If not, I will have to find a way of working around this.

    Moderator keesiemeijer

    (@keesiemeijer)

    No problem. I’m glad you got it resolved 🙂

    Moderator keesiemeijer

    (@keesiemeijer)

    Is there not a way of stopping certain images within this category from being pulled?

    No, I don’t think so.

    Why not use custom fields with the exact image url you want to use/not use for that post. That way you can use a normal query.

    http://codex.wordpress.org/Function_Reference/query_posts
    http://codex.wordpress.org/Function_Reference/WP_Query

    Thread Starter ched53

    (@ched53)

    I’ll look into it 🙂 Can’t thank you enough. Looking forward to seeing the site when it’s completed now.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Pulling attachments from specific post categories’ is closed to new replies.