Dạo này bận rộn quá nên cũng chẳng viết được bài nào. Tranh thủ viết bài cho đỡ trống blog (Viết xong lại off khoảng 1 tuần nữa). Nội dung bài viết này là gì? Thông thường khi viết bài bạn cần phải add custom field để hiển thị ảnh. Bài viết này sẽ hướng dẫn các bạn thực hiện việc này một cách tự động (bạn vẫn có thể dùng cách thủ công). Code được trích từ code của HuTek và được HT edit lại.

Bạn thêm đoạn code sau vào file custom_functions.php.

/********************************************************************
Get image attach
********************************************************************/
function img($width,$height) {
	global $post;
	$custom_field_value = get_post_meta($post->ID, 'thesis_post_image', true);
	$custom_field_value_3 = get_post_meta($post->ID, 'thesis_thumb', true);
	$custom_field_value_2 = get_post_meta($post->ID, 'out_image', true);
	$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1) );
	if ($custom_field_value == true) {
	$img_url = parse_url($custom_field_value,PHP_URL_PATH);
	print '<img src="'.get_template_directory_uri().'/custom/phpthumb/phpThumb.php?src='.$img_url.'&amp;w='.$width.'&amp;h='.$height.'&amp;zc=T&amp;q=70" alt="'.$post->post_title.'" />';
	}
	elseif ($custom_field_value_3 == true) {
	$img_url = parse_url($custom_field_value_3,PHP_URL_PATH);
	print '<img src="'.get_template_directory_uri().'/custom/phpthumb/phpThumb.php?src='.$img_url.'&amp;w='.$width.'&amp;h='.$height.'&amp;zc=T&amp;q=70" alt="'.$post->post_title.'" />';	
	}
	elseif ($custom_field_value_2 == true) {
	print '<img src="'.$custom_field_value_2.'" alt="'.$post->post_title.'" />';
	} 
	elseif ($attachments == true) {
		foreach($attachments as $id => $attachment) {
		$img = wp_get_attachment_image_src($id, 'full');
		$image = $image[0];
		$img_url = parse_url($img[0], PHP_URL_PATH);
		print '<img src="'.get_template_directory_uri().'/custom/phpthumb/phpThumb.php?src='.$img_url.'&amp;w='.$width.'&amp;h='.$height.'&amp;zc=T&amp;q=70" alt="'.$post->post_title.'" />';
		}
	}
	else {
		$img = get_template_directory_uri();
		$img_url = parse_url($img,PHP_URL_PATH);
		print '<img src="'.get_template_directory_uri().'/custom/phpthumb/phpThumb.php?src='.$img_url.'/custom/images/no_image.jpg&amp;w='.$width.'&amp;h='.$height.'&amp;zc=T&amp;q=70" alt="'.$post->post_title.'" />';
	}
}

Giải thích:

Trong đoạn code trên sẽ ưu tiên lấy ảnh trong phần post image của thesis, post thumb, ảnh từ host bên ngoài (sử dụng custom field out_image), cuối cùng là lấy ảnh đầu tiên trong bài viết.

Như vậy là xong phần lấy ảnh. Bây giờ để hiển thị ảnh thì tùy bạn thích đặt ở đâu thì bạn sử dụng openhook để thêm vào nhé. HT sẽ ví dụ mẫu một vị trí mà các bạn thường gặp nhất. Thêm ảnh thumb bên dưới tiêu đề bài viết teaser.

Để không hiển thị ảnh mặc định của thesis

Bạn edit file teasers.php trong lib/functions. Thay nội dung của dòng thứ 42 bằng đoạn sau:

$post_image = '';//thesis_post_image_info('thumb');

Thêm ảnh vào bên dưới tiêu đề bài viết teaser

Bạn thêm đoạn code sau vào file custom_functions.php.

/********************************************************************
Add picture teaser
********************************************************************/
add_action('thesis_hook_after_teaser_headline', 'thumb_teaser');
function thumb_teaser() { ?>
	<div class="thumb">
	<a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php img(100,100) ?></a>    
    </div>
<?php
}

Trong đoạn code trên bạn có thể thay 100 bằng giá trị khác để có ảnh lớn hoặc nhỏ hơn tùy thích. Bạn cũng có thể thêm CSS để cho nổi bật ảnh thumb hơn.

Chúc bạn thành công!

P/s: Do trong đoạn code trên có sử dụng phpThumb để resize ảnh nên bạn cần có phpThumb đặt bên trong thư mục custom nhé. phpThumb có thể tìm trên mạng hoặc đown tại đây.