WordPress non-regular get the first image of the article

Circulated onlineWordPressThe method of obtaining the first picture of the article as a thumbnail is achieved by using regular matching crawling,code show as below:

function catch_that_image() {
	global $post, $posts;
	$first_img = '';
	$output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches);
	$first_img = $matches[1][0];
		$first_img = '/images/default.jpg';
	return $first_img;

Some people say that this method is too inefficient,So I wrote the second method,First passget_children()Function to get the first picture of the articleID:

function get_first_image_ID($postID = '') {
	global $post;
	if( ! $postID ) $postID = $post->ID;
	$thumbnail_id = '';
	$args = array(
		'numberposts' => 1,
		'order'=> 'ASC',
		'post_mime_type' => 'image/jpeg',
		'post_parent' => $postID,
		'post_status' => null,
		'post_type' => 'attachment'
	$attachments = get_children( $args );
		$thumbnail_id = key($attachments);
	return $thumbnail_id;

If the above function is used in the main loop,No need to pass articlesID。

Then through the pictureIDGet picture address:

$thumbnail_src = wp_get_attachment_image_src($thumbnail_id, $size);
$img_url = $thumbnail_src[0];

$sizeThe parameter specifies the thumbnail version,Which is the size of the image。Below is the complete function to output the thumbnail:

function bzg_thumbnail($size='post-thumbnails') {
	global $post;
	$width = '';
	$height = '';
	$img_url = get_bloginfo('template_url') . '/images/default-thumbnail.png';
	if( has_post_thumbnail() ) {
		$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
	} else {
		$post_thumbnail_id = get_first_image_ID($post->ID);
	if($post_thumbnail_id) {
		$thumbnail_src = wp_get_attachment_image_src($post_thumbnail_id, $size);
		$img_url = $thumbnail_src[0];
		$width = ' width="' . $thumbnail_src[1] . '"';
		$height = ' height="' . $thumbnail_src[2] . '"';
	echo '<a href="' . get_permalink($post->ID) . '">';
	echo '<img src="' . $img_url . '"' . $width . $height . ' alt="' . $post->post_title . '" />';
	echo '</a>';

When a featured image is specified,Thumbnails will output featured images,Otherwise, get the first picture of the article as a thumbnail,If the article does not upload any pictures,ThemeimagesIn the catalogdefault-thumbnail.pngPicture as thumbnail。

Use the method of this article to get the thumbnail,You can choose a small size version of the picture,The regular way is to output the original image,Will have a certain impact on the access speed of the article list page。

Related Article
WordPress comment date tag: comment_date

WordPress template tags comment_ Date is used to output the date of a comment, usually in a custom comment template. Read more

WordPress comment summary tag: comment_excerpt

WordPress template tags comment_ Excerpt is used to output the summary of comments. The default output is 20 characters. It Read more

WordPress comment time tag: comment_time

WordPress template tags comment_ Time is used to output the publishing time of the current comment, which is the same Read more

WordPress RSS format comment author name tag: comment_author_rss

WordPress template tags comment_ author_ RSS is used to output the comment author name in RSS format, which has no Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image