WordPress的特色图像是一个很实用的功能,为每篇文章增加一个特色图像,可以使blog各个部分都更生动。比如首页每篇文章都有自己的缩略图,相关文章中用缩略图告诉用户这些文章的主题,或者在侧栏加一个特色文章功能,显示文章特色图像。
然而有时候,我们在文章上上传了图片,却忘记了上传缩略图。这时候有些需要显示缩略图的主题显示可能就不正常了,这当然是我们不希望看到的。下面的一段代码可以自动提取文章里面的第一张图片作为文章的缩略图。
自动提取第一张图片为首页缩略图的函数
<pre class="prettyprint linenums">/* 抓取文章第一张图片作为特色图片 */ function autoset_featured() { global $post; $already_has_thumb = has_post_thumbnail($post - >ID); if (!$already_has_thumb) { $attached_image = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1"); if ($attached_image) { foreach($attached_image as $attachment_id = >$attachment) { set_post_thumbnail($post - >ID, $attachment_id); } } } } //end function add_action('the_post', 'autoset_featured'); add_action('save_post', 'autoset_featured'); add_action('draft_to_publish', 'autoset_featured'); add_action('new_to_publish', 'autoset_featured'); add_action('pending_to_publish', 'autoset_featured'); add_action('future_to_publish', 'autoset_featured');
以上获取文章缩略图包含了三个功能:
如果文章存在缩略图则直接读取缩略图。
如果不存在缩略图则读取文章第一张图片为缩略图。
如果文章美图图片则读取指定的默认图片,本文以(/images/thumbnail.jpg)为例。
//缩略图获取 function get_the_thumbnail() { global $post; if (has_post_thumbnail ()) { //如果存在缩略图读取之 echo '<a class="pic" title="'.trim ( strip_tags ( $post->post_title ) ).'" href="'%20.%20get_permalink%20()%20.%20'">'; $domsxe = simplexml_load_string ( get_the_post_thumbnail () ); $thumbnailsrc = $domsxe->attributes()->src; echo '<img src="'%20.%20$thumbnailsrc%20.%20'" alt="' . trim ( strip_tags ( $post->post_title ) ) . '" />'; echo '</a>'; } else { //读取文章第一张图片为缩略图 $content = $post->post_content; preg_match_all ( '/<img.*?(?: |\t|\r|\n)?src=['"]?(.+?)['"]?(?:(?: |\t|\r|\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER ); $n = count ( $strResult [1] ); if ($n > 0) { //文章第一张图片 echo '<a class="pic" title="'.trim ( strip_tags ( $post->post_title ) ).'" href="'%20.%20get_permalink%20()%20.%20'"><img src="'%20.%20$strResult%20[1]%20[0]%20.%20'" alt="" /></a>'; } else { //如果文章没有图片则读取默认图片 echo '<a class="pic" title="'.trim ( strip_tags ( $post->post_title ) ).'" href="'%20.%20get_permalink%20()%20.%20'"><img src="'%20.%20get_bloginfo%20(%20'template_url'%20)%20.%20'/images/thumbnail.jpg" alt="" /></a>'; } } }
再分享下实现文章不设置特色图片时文章使用外链图片时显示随机缩略图的代码
function _get_post_thumbnail($size = 'thumbnail', $class = 'thumb') { $html = ''; if (has_post_thumbnail()) { /*$domsxe = simplexml_load_string(get_the_post_thumbnail()); $src = $domsxe->attributes()->src; $src_array = wp_get_attachment_image_src(_get_attachment_id_from_src($src), $size); $html = sprintf('<img class="%s" alt="" data-src="%s" />', $src_array[0], $class);*/ $domsxe = get_the_post_thumbnail(); // print_r($domsxe); preg_match_all('/<img.*?(?: |\t|\r|\n)?src=['"]?(.+?)['"]?(?:(?: |\t|\r|\n)+.*?)?>/sim', $domsxe, $strResult, PREG_PATTERN_ORDER); $images = $strResult[1]; foreach($images as $src) { $html = sprintf('<img class="thumb" alt="" data-src="%s" />', $src); break; } } else { $random = mt_rand(1, 10); $html = sprintf('<img class="%s" alt="" data-src="%s" />', get_stylesheet_directory_uri().'/img/random/'.$random.'.jpg', $class); } return $html; }
覆盖同名函数,然后添加图片.jpg到img/random即可。
通过以上的这种方法就可以完美的实现WordPress自动调用文章图片为特色图像,如果你有什么好的建议,也请留言给我。
评论前必须登录!
注册