/** * Image post format custom output */ function duena_post_format_image() { global $post; if (function_exists('get_post_format_meta')) { $meta = get_post_format_meta($post->ID); } if (!empty($meta['image'])) { $att_id = img_html_to_post_id($meta['image']); $image = wp_get_attachment_image($att_id, array(770, 295)); $cur_url = wp_get_attachment_url($att_id, false); } elseif (has_post_thumbnail()) { $post_thumbnail_id = get_post_thumbnail_id(); $image = wp_get_attachment_image($post_thumbnail_id, array(770, 295)); $cur_url = wp_get_attachment_url($post_thumbnail_id, false); } else { $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID, 'orderby' => 'menu_order', 'order' => 'asc'); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $attachment) { $image = wp_get_attachment_image($attachment->ID, array(770, 295)); $cur_url = wp_get_attachment_url($attachment->ID, false); } } } if (isset($cur_url)) { echo '<a href="' . $cur_url . '" class="lightbox_img single" data-effect="mfp-zoom-in">' . $image . '</a>'; } }
/** * Return the first image in the current (@global) post's content * * @since 3.6.0 * * @param string $attached_size If an attached image is found, the size to display it. * @param WP_Post $post Optional. Used instead of global $post when passed. */ function get_the_post_format_image($attached_size = 'full', &$post = null) { if (empty($post)) { $post = get_post(); } if (empty($post)) { return ''; } $cache_key = "image:{$attached_size}"; if (isset($post->format_content[$cache_key])) { return $post->format_content[$cache_key]; } if (!isset($post->format_content)) { $post->format_content = array(); } $matched = false; $meta = get_post_format_meta($post->ID); $link_fmt = '%s'; if (!empty($meta['url'])) { $link_fmt = '<a href="' . esc_url($meta['url']) . '">%s</a>'; } if (!empty($meta['image'])) { if (is_numeric($meta['image'])) { $image = wp_get_attachment_image(absint($meta['image']), $attached_size); // wrap image in <a> if (!empty($meta['url'])) { $image = sprintf($link_fmt, $image); } } elseif (has_shortcode($meta['image'], 'caption')) { // wrap <img> in <a> if (!empty($meta['url']) && false === strpos($meta['image'], '<a ')) { $meta['image'] = preg_replace('#(<img[^>]+>)#', sprintf('<a href="%s">$1</a>', esc_url($meta['url'])), $meta['image']); } $attachment_id = img_html_to_post_id($meta['image'], $matched_html); if ($attachment_id && $matched_html) { $meta['image'] = str_replace($matched_html, wp_get_attachment_image($attachment_id, $attached_size), $meta['image']); $attachment = wp_get_attachment_image_src($attachment_id, $attached_size); $attachment_width = !empty($attachment[1]) ? $attachment[1] : 0; if ($attachment_width && preg_match_all('#width=([\'"])(.+?)\\1#is', $meta['image'], $matches) && !empty($matches)) { foreach ($matches[2] as $width) { if ($width != $attachment_width) { $meta['image'] = str_replace($matches[0], sprintf('width="%d"', $attachment_width), $meta['image']); } } } } $image = do_shortcode($meta['image']); } elseif (!preg_match('#<[^>]+>#', $meta['image'])) { // not HTML, assume URL $attachment_id = attachment_url_to_postid($meta['image']); if ($attachment_id) { $image = wp_get_attachment_image($attachment_id, $attached_size); } else { $image = sprintf('<img src="%s" alt="" />', esc_url($meta['image'])); } } else { // assume HTML $image = $meta['image']; $attachment_id = img_html_to_post_id($image, $matched_html); if ($attachment_id && $matched_html) { $image = str_replace($matched_html, wp_get_attachment_image($attachment_id, $attached_size), $image); } } if (false === strpos($image, '<a ')) { $post->format_content[$cache_key] = sprintf($link_fmt, $image); } else { $post->format_content[$cache_key] = $image; } return $post->format_content[$cache_key]; } $medias = get_attached_images($post->ID); if (!empty($medias)) { $media = reset($medias); $sizes = get_intermediate_image_sizes(); $sizes[] = 'full'; // Add original image source. $urls = array(); foreach ($sizes as $size) { $image = wp_get_attachment_image_src($media->ID, $size); if ($image) { $urls[] = reset($image); } } // Add media permalink. $urls[] = get_attachment_link($media->ID); $count = 1; $content = $post->post_content; if (preg_match_all('/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER) && !empty($matches)) { foreach ($matches as $shortcode) { if ('caption' === $shortcode[2]) { foreach ($urls as $url) { if (strstr($shortcode[0], $url)) { if (!$matched) { $matched = do_shortcode($shortcode[0]); } $content = str_replace($shortcode[0], '', $content, $count); } } } } } foreach (array('a', 'img') as $tag) { if (preg_match_all('#' . get_tag_regex($tag) . '#', $content, $matches, PREG_SET_ORDER) && !empty($matches)) { foreach ($matches as $match) { foreach ($urls as $url) { if (strstr($match[0], $url)) { if (!$matched) { $matched = $match[0]; } $content = str_replace($match[0], '', $content, $count); } } } } } $post->split_content = $content; if (!$matched) { $image = wp_get_attachment_image($media->ID, $attached_size); $post->format_content[$cache_key] = sprintf($link_fmt, $image); } else { $post->format_content[$cache_key] = $matched; if (!empty($meta['url']) && false === stripos($matched, '<a ')) { $post->format_content[$cache_key] = sprintf($link_fmt, $matched); } } return $post->format_content[$cache_key]; } $content = $post->post_content; $htmls = get_content_images($content, true, true, 1); if (!empty($htmls)) { $html = reset($htmls); $attachment_id = img_html_to_post_id($html, $matched_html); if ($attachment_id && $matched_html) { $html = str_replace($matched_html, wp_get_attachment_image($attachment_id, $attached_size), $html); } $post->split_content = $content; $post->format_content[$cache_key] = sprintf($link_fmt, $html); return $post->format_content[$cache_key]; } }
function wp_rp_get_attached_img_url($related_post, $size) { $extracted_image = get_post_meta($related_post->ID, '_wp_rp_image', true); if ($extracted_image === 'empty') { return false; } $image_data = wp_rp_get_image_data((int) $extracted_image); if (!$image_data && $extracted_image) { // image_id in the db is incorrect delete_post_meta($related_post->ID, '_wp_rp_image'); } if (!$image_data && has_post_thumbnail($related_post->ID)) { $image_data = wp_rp_get_image_data(get_post_thumbnail_id($related_post->ID)); } if (!$image_data && function_exists('get_post_format_meta') && function_exists('img_html_to_post_id')) { // WP 3.6 Image post format. Check wp-includes/media.php:get_the_post_format_image for the reference. $meta = get_post_format_meta($related_post->ID); if (!empty($meta['image'])) { if (is_numeric($meta['image'])) { $image_id = absint($meta['image']); } else { $image_id = img_html_to_post_id($meta['image']); } $image_data = wp_rp_get_image_data($image_id); } } if (!$image_data) { wp_rp_extract_images_from_post($related_post); return false; } if ($img_src = wp_rp_get_image_with_exact_size($image_data, $size)) { return $img_src['url']; } wp_rp_extract_images_from_post($related_post, $image_data['id']); return false; }