/** * Get related posts attachments data slightly modified. * * @return array Attachments data. */ function presscore_get_related_posts($options = array()) { $default_options = array('select' => 'only', 'exclude_current' => true, 'args' => array()); $options = wp_parse_args($options, $default_options); // exclude current post if in the loop if (in_the_loop() && $options['exclude_current']) { $options['args'] = array_merge($options['args'], array('post__not_in' => array(get_the_ID()))); } $posts = presscore_get_posts_in_categories($options); update_post_thumbnail_cache($posts); $attachments_ids = array(); $attachments_data_override = array(); $posts_data = array(); // get posts attachments id if ($posts->have_posts()) { global $post; $post_back = $post; while ($posts->have_posts()) { $posts->the_post(); // thumbnail or first attachment id if (has_post_thumbnail()) { $attachment_id = get_post_thumbnail_id(); } else { if ($attachment = presscore_get_first_image()) { $attachment_id = $attachment->ID; } else { $attachment_id = 0; } } switch (get_post_type()) { case 'post': $post_meta = presscore_new_posted_on('post'); break; case 'dt_portfolio': $post_meta = presscore_new_posted_on('dt_portfolio'); break; default: $post_meta = presscore_new_posted_on(); } $post_data = array(); ///////////////////////// // attachment data // ///////////////////////// $post_data['full'] = $post_data['width'] = $post_data['height'] = ''; $meta = wp_get_attachment_image_src($attachment_id, 'full'); if (!empty($meta)) { $post_data['full'] = esc_url($meta[0]); $post_data['width'] = absint($meta[1]); $post_data['height'] = absint($meta[2]); } $post_data['thumbnail'] = wp_get_attachment_image_src($attachment_id, 'thumbnail'); $post_data['caption'] = ''; $post_data['video_url'] = esc_url(get_post_meta($attachment_id, 'dt-video-url', true)); $post_data['mime_type_full'] = get_post_mime_type($attachment_id); $post_data['mime_type'] = dt_get_short_post_myme_type($attachment_id); $post_data['ID'] = $attachment_id; $post_data['image_attachment_data'] = array('caption' => $post_data['caption'], 'description' => wp_kses_post(get_post_field('post_content', $attachment_id)), 'title' => presscore_imagee_title_is_hidden($attachment_id) ? '' : get_the_title($attachment_id), 'permalink' => get_permalink($attachment_id), 'video_url' => $post_data['video_url'], 'ID' => $attachment_id); /////////////////// // post data // /////////////////// $post_data['title'] = get_the_title(); $post_data['permalink'] = get_permalink(); $post_data['link'] = presscore_get_project_link('project-link'); $post_data['description'] = get_the_excerpt(); $post_data['alt'] = get_the_title(); $post_data['parent_id'] = get_the_ID(); $post_data['meta'] = $post_meta; // save data $posts_data[] = $post_data; } $post = $post_back; setup_postdata($post); } return $posts_data; }
/** * Get related posts attachments data slightly modified. * * @return array Attachments data. */ function presscore_get_related_posts($options = array()) { $default_options = array('select' => 'only', 'exclude_current' => true, 'args' => array()); $options = wp_parse_args($options, $default_options); // exclude current post if in the loop if (in_the_loop() && $options['exclude_current']) { $options['args'] = array_merge($options['args'], array('post__not_in' => array(get_the_ID()))); } $posts = presscore_get_posts_in_categories($options); $attachments_ids = array(); $attachments_data_override = array(); $posts_data = array(); // get posts attachments id if ($posts->have_posts()) { while ($posts->have_posts()) { $posts->the_post(); // thumbnail or first attachment id if (has_post_thumbnail()) { $attachments_ids[] = get_post_thumbnail_id(); } else { if ($attachment = presscore_get_first_image()) { $attachments_ids[] = $attachment->ID; } else { $attachments_ids[] = 0; } } switch (get_post_type()) { case 'post': $post_meta = presscore_new_posted_on('post'); break; case 'dt_portfolio': $post_meta = presscore_new_posted_on('dt_portfolio'); break; default: $post_meta = presscore_new_posted_on(); } $attachments_data_override[] = array('permalink' => get_permalink(), 'link' => presscore_get_project_link('project-link'), 'title' => get_the_title(), 'description' => get_the_excerpt(), 'alt' => get_the_title(), 'parent_id' => get_the_ID(), 'meta' => $post_meta); } wp_reset_postdata(); } if ($attachments_ids) { // what we want $attachments_data = presscore_get_attachment_post_data($attachments_ids); // what we get $attachments_data_ids = array(); if ($attachments_data) { $attachments_data_ids = wp_list_pluck($attachments_data, 'ID'); } $default_image = presscore_get_default_image(); foreach ($attachments_ids as $key => $id) { $attachments_data_key = array_search($id, $attachments_data_ids); // if there are image - add it to array if (false !== $attachments_data_key) { $posts_data[$key] = $attachments_data[$attachments_data_key]; // or add noimage } else { $posts_data[$key] = array('full' => $default_image[0], 'width' => $default_image[1], 'height' => $default_image[2]); } if (isset($attachments_data_override[$key])) { $posts_data[$key] = array_merge($posts_data[$key], $attachments_data_override[$key]); } } } return $posts_data; }