摘抄:WordPress获取第一张图片作为缩略图的几种方法

摘抄:WordPress获取第一张图片作为缩略图的几种方法 | JUST FOR FUN

1.君子不器版

首先判断文章内是否设置了特色图像,如果有则输出特色图像的缩略图,因为如果你设置了特色图像,那么肯定是想要展示这一张图片,所以这个应该排在第一位。
如果文章中没有特色图像,那么就判断文章中是否有图片(外链图片也一样),如果有则输出获取到的第一张图片作为文章的缩略图。
如果文章中连图片都没有,则输出一张提前设置好的图片或者不输出。
复制下面的代码,然后将该代码放入你当前主题的模版函数(functions.php)文件中保存即可。

#输出缩略图
function junzibuqi_com_get_img() {
        global $post;
        if (has_post_thumbnail ()) {
                //如果存在缩略图
                $domsxe = simplexml_load_string ( get_the_post_thumbnail () );
                $thumbnailsrc = $domsxe->attributes()->src;
                echo "<a href=\"" . get_permalink() . "\">" ;
                echo '<img data-src="' . $thumbnailsrc . '" class="thumb">';
                echo '</a>';
        } else {
                //读取第一张图片
                $content = $post->post_content;
                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 "<a href=\"" . get_permalink() . "\">" ;
                        echo '<img src="' . $strResult [1] [0] . '" class="thumb">';
                        echo '</a>';
                } else {
                        //如果文章没有图片则读取默认图片
                        echo "<a class=\"focus\" href=\"" . get_permalink() . "\">" ;
                        echo '<img src="' . get_stylesheet_directory_uri() . '"/img/thumbnail.png" referrerpolicy="no-referrer" class="thumb">';
                        echo '</a>';
                }
        }
}

调用方式

请复制下面的调用代码,放在你想要显示缩略图的位置即可

<?php junzibuqi_com_get_img();?>

2.水景一页版

将下面的函数放到主题的 functions.php 文件中(如果没有就自己创建一个),然后在需要的时候调用这个函数来获得相应图片的 url 地址。在调用该函数的时候记得将文章的 $id 传递过去。

function catch_the_image( $id ) {
// global $post, $posts;
$first_img = '';
// 如果设置了缩略图
$post_thumbnail_id = get_post_thumbnail_id( $id );
if ( $post_thumbnail_id ) {
    $output = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
    $first_img = $output[0];
}
else { // 没有缩略图,查找文章中的第一幅图片
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];
    if(empty($first_img)){ // 既没有缩略图,文中也没有图,设置一幅默认的图片
        $first_img = "http://yourdomain.tld/images/default.jpg";
    }
}
return $first_img;
}

该函数返回的是图片的 url 地址。
在需要显示图片的地方输出图片:

<?php echo catch_that_image() ?>

3.暗淡的黑版

方法1.无视任何设置,直接获取文章中第一张图片并显示;如果文章无图片,获取自定义图片并显示。
①将以下代码粘贴到functions.php文件中。

function catch_that_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] [0];
//如果文章无图片,获取自定义图片
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
//请自行设置一张default.jpg图片
}
return $first_img;
}

②在需要显示图片的地方输出图片:

<?php echo catch_that_image() ?>

方法2.如果我们手动设置了特色图像,而且文章中又有图片,那该如何显示呢?我们自此可以使用逻辑判断,给他们一个优先条件。此方法网上代码重复太多,已经不知道原创是谁了…

function catch_that_image( $id ) {
// global $post, $posts;
$first_img = '';
// 如果设置了缩略图
$post_thumbnail_id = get_post_thumbnail_id( $id );
if ( $post_thumbnail_id ) {
    $output = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
    $first_img = $output[0];
}
else { // 没有缩略图,查找文章中的第一幅图片
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];
    if(empty($first_img)){ // 既没有缩略图,文中也没有图,设置一幅默认的图片
        $first_img = "/images/default.jpg";
    }
}
return $first_img;
}

4.wp酷版

替换原本特色图像功能,设置外链缩略图
Functions.php:

add_theme_support( 'post-thumbnails' );
function catch_that_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] [0];
if(empty($first_img)){ //Defines a default image
$popimg=get_option( 'mao10_popimg');
$first_img = "$popimg";
}
return $first_img;
}
function mmimg($postID) {
 $cti = catch_that_image();
 $showimg = $cti;
 has_post_thumbnail();
 if ( has_post_thumbnail() ) {
 $thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail');
 $shareimg = $thumbnail_image_url[0];
 } else {
 $shareimg = $showimg;
 };
 return $shareimg;
}

调用方式:

<img src="<?php echo mmimg(get_the_ID()); ?>" alt="" />

5,云落版

自动选取文字的第一个图片,然后设为WordPress自动添加特色图片

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           }
                        }
      }  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

使用方法
将代码添加进function.php即可,注意要在?>之前
看了下 基本思路都是一样的,照着做就行。

WP技巧代码·功能

解决使用七牛CDN导致AJAX评论报错{“ERROR”:”GET FROM IMAGE SOURCE FAILED: E405″}

2015-12-3 14:24:21

WP技巧代码·功能

.htaccess禁止指定文件夹/后缀文件下载/扫描

2015-12-19 16:33:12

个人中心
今日签到
有新私信 私信列表
搜索