V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Beta
V2EX  ›  WordPress

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

  •  
  •   Beta · 2013-03-23 17:00:33 +08:00 · 4242 次点击
    这是一个创建于 4053 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有没有这样功能的插件?
    8 条回复    1970-01-01 08:00:00 +08:00
    ihuguowei
        1
    ihuguowei  
       2013-03-23 17:34:53 +08:00
    我的笨方法是通过文章摘要设置。
    sobigfish
        2
    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
        3
    gamecmt  
       2013-03-23 17:41:59 +08:00
    试试这个插件
    WordPress图片插件 Thumbnails Anywhere
    foru17
        4
    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
        5
    liyandong  
       2013-03-23 22:01:57 +08:00   ❤️ 1
    Sivan
        6
    Sivan  
       2013-03-23 22:04:38 +08:00
    很简单,改下 function.php 开启 add_theme_support( 'post-thumbnails' );
    然后在 index.php 的适当位置写入调用函数即可。
    goxofy
        7
    goxofy  
       2013-03-23 22:05:23 +08:00 via iPhone
    要我说就<!--more-->
    ibolee
        8
    ibolee  
       2013-09-03 10:53:40 +08:00
    mark~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2980 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 14:16 · PVG 22:16 · LAX 07:16 · JFK 10:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.