代码实现文章缩略图的方法
1 | https://www.91wordpress.com/703.html |
1 2 | cd /home/wwwroot/default/vermaxcn/wp-content/themes/flatsome/ vi functions.php |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | add_theme_support( 'post-thumbnails' ); function post_thumbnail( $width = 255,$height = 130 ){ global $post; if( has_post_thumbnail() ){ $timthumb_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full'); $post_timthumb = '<img src="'.get_bloginfo("template_url").'/timthumb.php?src='.$timthumb_src[0].'&h='.$height.'&w='.$width.'&zc=1" alt="'.$post->post_title.'" class="thumb" title="'.get_the_title().'"/>'; echo $post_timthumb; } else { $content = $post->post_content; $defaltthubmnail = sapphire('default-thumb') ? sapphire('default-thumb') : get_template_directory_uri().'/img/no-thumb.png'; 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 '<img src="'.get_bloginfo("template_url").'/timthumb.php?w='.$width.'&h='.$height.'&src='.$strResult[1][0].'" title="'.get_the_title().'" alt="'.get_the_title().'"/>'; } else { echo '<img src="'.get_bloginfo("template_url").'/timthumb.php?w='.$width.'&h='.$height.'&src='.$defaltthubmnail.'" title="'.get_the_title().'" alt="'.get_the_title().'"/>'; } } } |
注:此处需要用到一个裁剪缩略图的PHP文件,位于 : https://webcode.bndstone.com/timthumb.php
1 | cp /home/wwwroot/default/webcode/timthumb.php /home/wwwroot/default/vermaxcn/wp-content/themes/flatsome/timthumb.php |
2. 在需要显示文章缩略图的地方,添加以下代码:
1 | <?php post_thumbnail(145,145) ?> |
备注: 2. 在需要显示文章缩略图的地方,添加以下代码: 还没测试