wordpress首页上的文章如何设置只显示每篇文章的第一张图片

2013-03-23 17:00:33 +08:00
 Beta
有没有这样功能的插件?
4256 次点击
所在节点    WordPress
8 条回复
ihuguowei
2013-03-23 17:34:53 +08:00
我的笨方法是通过文章摘要设置。
sobigfish
2013-03-23 17:40:02 +08:00
设置Featured Image 并显示不好么,但应该有lz想要的那种 先找Featured Image 没有的情况下读第1张图。 http://wordpress.org/extend/plugins/search.php?q=feature+image
gamecmt
2013-03-23 17:41:59 +08:00
试试这个插件
WordPress图片插件 Thumbnails Anywhere
foru17
2013-03-23 18:58:25 +08:00
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}

function get_first_image($post)
{
$PostContent = $post->post_content;

preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $PostContent, $PostImg);

$ImgNumber = count($PostImg[0]);

for($i=0;$i<=$ImgNumber;$i++)
{
$img_src=$PostImg[1][$i];
if(img_exists($img_src)) //第一个有效图片
{
if (eregi("flickr.com",$img_src))
{
$img_url = str_replace(".jpg", "_s.jpg", $img_src);
}else
{
$img_url=$img_src;
$output .= '<a href="'.get_permalink($post->ID).'" title="'.wptexturize($post->post_title).'"><img src="' . $img_url . '" /></a>';
echo $output;
return 1;
}

}
}


//输出默认的缩略图地址
?>
<a href="<?php the_permalink()?>"><img src="<?php bloginfo('template_url');?>/images/no-thumb.jpg" title="<?php the_title();?>" alt="<?php the_title();?>"/></a>

<?php

return 0;
}

//PHP判断远程图片是否存在
function img_exists($url)
{
$head=@get_headers($url);
if(!is_array($head))
return false;

if(file_get_contents($url,0,null,0,1))
return 1;
else
return 0;
}

}
liyandong
2013-03-23 22:01:57 +08:00
Sivan
2013-03-23 22:04:38 +08:00
很简单,改下 function.php 开启 add_theme_support( 'post-thumbnails' );
然后在 index.php 的适当位置写入调用函数即可。
goxofy
2013-03-23 22:05:23 +08:00
要我说就<!--more-->
ibolee
2013-09-03 10:53:40 +08:00
mark~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/63827

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX