/** * 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]; } }
</div> </div> <?php foreach ($this->content as $content) { ?> <div class="thumbnail" style="width:270px;"> <!-- <div class="panel-heading" style="text-align:center"> <div class="btn-group btn-group-sm"> <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-resize-small"></span> 拉取</button> <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-resize-full"></span> 推送</button> </div> </div> --> <div class="imgs"> <img width="250" src="<?php echo get_content_images($content->content_text, true); ?> " /> </div> <div class="caption"> <div class="title"><?php echo $content->content_tile; ?> </div> <div class="content"> <?php echo $content->content_description; ?> </div> <div class="author"> <?php