Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $category_ID = 25;
- global $wpdb;
- // get the first 18 posts with an attachment from the database
- $myposts = $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($myposts){
- foreach($myposts 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>';
- }
- }
- wp_reset_postdata();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement