麻烦高手帮忙看一下下面的代码,这个代码功能可以把文章的第一张图片自动设置为缩略图,但是实际使用起来,却是把最后一张图片设置为缩略图,对于文科生来讲,实在改不来,有没有高手能帮忙看以下,是哪里出错了,谢谢先!
//添加缩略图支持 if ( function_exists( 'add_theme_support' ) ) { add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size(200, 200, true); add_image_size( 'post-thumbnail', 200, 200, true ); //侧边栏 }
/wordpress 抓取文章第一张图片为缩略图函数/ function catch_first_image(){ global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src='"['"].*>/i', $post->post_content, $matches); $first_img = $matches [1]; if(empty($first_img)){ //Defines a default image $first_img ="0"; } return $first_img; };
//输出缩略图地址 function post_thumbnail_src(){ global $post; if( $values = get_post_custom_values("thumb_value") ) { //输出自定义域图片地址 $post_thumbnail_src = $values [0]; }
elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'post-thumbnail');
$post_thumbnail_src = $thumbnail_src [0];
}
elseif( $values=catch_first_image() ){ //如果有在线图片,选择在线图片
$values = $values=catch_first_image() ;
$post_thumbnail_src = $values [0];
}
else {
$post_thumbnail_src = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$post_thumbnail_src = $matches[1][0]; //获取该图片 src
if(empty($post_thumbnail_src)){
echo get_bloginfo('template_url');
echo '/images/default-thumb.jpg';//如果日志中没有图片,则显示默认图片
}
};
echo $post_thumbnail_src;
}
1
wapptm OP 晕倒,没人帮忙啊
|
2
Jakesoft 2016-09-27 23:20:48 +08:00
没仔细看, $matches[1] 换成 $matches[0] 试试?
|
3
popu111 2016-09-28 00:04:45 +08:00 via Android
上半排版略乱,估计大部分人一进来就被恶心出去了_(:з」∠)_不说了我也水贴子去了
|
4
lslqtz 2016-09-28 01:06:59 +08:00
我选择 QQWorld 。
https://www.loliwiki.org/wp-content/uploads/2016/09/QQ 截图 20160928010610.png |
5
lslqtz 2016-09-28 01:07:05 +08:00
|
7
wapptm OP 重新发试试
//添加缩略图支持 if ( function_exists( 'add_theme_support' ) ) { add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size(200, 200, true); add_image_size( 'post-thumbnail', 200, 200, true ); //侧边栏 } /*wordpress 抓取文章第一张图片为缩略图函数*/ function catch_first_image(){ global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1]; if(empty($first_img)){ //Defines a default image $first_img ="0"; } return $first_img; }; //输出缩略图地址 function post_thumbnail_src(){ global $post; if( $values = get_post_custom_values("thumb_value") ) { //输出自定义域图片地址 $post_thumbnail_src = $values [0]; } elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址 $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'post-thumbnail'); $post_thumbnail_src = $thumbnail_src [0]; } elseif( $values=catch_first_image() ){ //如果有在线图片,选择在线图片 $values = $values=catch_first_image() ; $post_thumbnail_src = $values [0]; } else { $post_thumbnail_src = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $post_thumbnail_src = $matches[1][0]; //获取该图片 src if(empty($post_thumbnail_src)){ echo get_bloginfo('template_url'); echo '/images/default-thumb.jpg';//如果日志中没有图片,则显示默认图片 } }; echo $post_thumbnail_src; } |