13071077710
您的当前位置: 首页 > wordpress > wordpress获取文章中的图片

wordpress获取文章中的图片

时间: 2017-11-22

//功能集成 | 开启特色图片
if ( function_exists('add_theme_support') )add_theme_support('post-thumbnails');
//输出缩略图地址
function Zixie_image(){
    global $post;
    if( $values = get_post_custom_values("thumb") ) {   //输出自定义域图片地址
        $values = get_post_custom_values("thumb");
        $Zixie_image = $values [0];
    } elseif( has_post_thumbnail() ){    //如果有特色缩略图,则输出缩略图地址
        $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
        $Zixie_image = $thumbnail_src [0];
    } else {
        $Zixie_image = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        if(!empty($matches[1][0])){
            $Zixie_image = $matches[1][0];   //获取该图片 src
        }elseif( zixieme('zixieme_post_thumbnail') ){
            $Zixie_image = zixieme('zixieme_post_thumbnail');
        }else{  
            //如果日志中没有图片,则显示随机图片
            //$random = mt_rand(1, 5);
            //$Zixie_image = get_template_directory_uri().'/img/random/'.$random.'.jpg';
            //如果日志中没有图片,则显示默认图片
            $Zixie_image = get_template_directory_uri().'/img/default_thumb.png';
        }
    }
    return $Zixie_image;
} 

function get_post_thumbnail_url($post_id){
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    $post=get_post($post_id);
    if( has_post_thumbnail() ){    //如果有特色缩略图,则输出缩略图地址
        $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id),'full');
        $Zixie_image = $thumbnail_src [0];
    } else {
        $Zixie_image = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        if(!empty($matches[1][0])){
            $Zixie_image = $matches[1][0];   //获取该图片 src
        }else{  
            $Zixie_image = '';
        }
    }
    return $Zixie_image;
}

其中zixieme(‘zixieme_post_thumbnail’) 的使用参考后台选项开发框架 Options Framework

Copyright © 2013-2018 子歇    版权所有   网站地图