我们在使用WP建站时,通常会遇到一个需求,就是文章特色图片封面的问题,有时候转载没有图片的文章,这样在我们展示时显得只用固定缩略图会不够美观,今天七赞云(7an.com)站长就与大家分享这个方法。

下面是优化后的代码(更智能):

最终推荐代码(复制到 functions.php 底部)

PHP

// ==================== 智能随机默认特色图片 ====================
function auto_random_featured_image_if_no_images($post_id) {
    // 如果已经是修订版本或已设置特色图片,则跳过
    if (wp_is_post_revision($post_id) || has_post_thumbnail($post_id)) {
        return;
    }

    $post = get_post($post_id);
    $content = $post->post_content;

    // 检查正文中是否包含图片(img标签 或 wp-image-)
    $has_image_in_content = preg_match('/<img[^>]+>/i', $content) || 
                            preg_match('/wp-image-\d+/', $content);

    // 如果正文中已经有图片,则不设置默认图
    if ($has_image_in_content) {
        return;
    }

    // 默认图片库(支持多张随机)
    $default_images = array(
        'https://你的域名/wp-content/uploads/2026/07/cover1.jpg',
        'https://你的域名/wp-content/uploads/2026/07/cover2.jpg',
        'https://你的域名/wp-content/uploads/2026/07/cover3.jpg',
        'https://你的域名/wp-content/uploads/2026/07/cover4.jpg',
        // 添加更多图片...
    );

    $random_url = $default_images[array_rand($default_images)];

    $attachment_id = attachment_url_to_postid($random_url);
    
    if ($attachment_id) {
        set_post_thumbnail($post_id, $attachment_id);
    }
}

// 触发时机
add_action('save_post', 'auto_random_featured_image_if_no_images', 10, 1);
add_action('wp_insert_post', 'auto_random_featured_image_if_no_images', 10, 1);

 

如果你只想对某个特定分类生效的话,请使用下面这段代码,请记得修改分类ID:
// ==================== 只对特定分类生效的随机默认特色图片 ====================
function auto_random_featured_image_by_category($post_id) {
    // 跳过修订版和已设置特色图的文章
    if (wp_is_post_revision($post_id) || has_post_thumbnail($post_id)) {
        return;
    }

    $post = get_post($post_id);
    $content = $post->post_content;

    // 如果正文中已有图片,则不设置
    if (preg_match('/<img[^>]+>/i', $content) || preg_match('/wp-image-\d+/', $content)) {
        return;
    }

    // ==================== 这里设置你要生效的分类 ====================
    $target_categories = array(5, 8, 12);   // ← 修改这里!填分类ID
    
    // 获取文章所属分类ID
    $post_categories = wp_get_post_categories($post_id);
    
    // 如果文章不属于目标分类,则跳过
    if (!array_intersect($target_categories, $post_categories)) {
        return;
    }

    // 默认图片库(支持多张随机)
    $default_images = array(
        'https://你的域名/wp-content/uploads/2026/07/cover1.jpg',
        'https://你的域名/wp-content/uploads/2026/07/cover2.jpg',
        'https://你的域名/wp-content/uploads/2026/07/cover3.jpg',
        'https://你的域名/wp-content/uploads/2026/07/cover4.jpg',
        // 可以继续添加更多
    );

    $random_url = $default_images[array_rand($default_images)];
    $attachment_id = attachment_url_to_postid($random_url);

    if ($attachment_id) {
        set_post_thumbnail($post_id, $attachment_id);
    }
}

// 触发
add_action('save_post', 'auto_random_featured_image_by_category', 10, 1);
add_action('wp_insert_post', 'auto_random_featured_image_by_category', 10, 1);

 

这样我们的站点就会显得智能多了,让大家记住本站,有问题和站长多多交流。


使用说明

  1. 把代码中的 https://你的域名/... 替换成你实际上传的图片链接。
  2. 保存 functions.php。
  3. 新建或更新一篇没有特色图片且正文中没插图的文章,即可看到效果。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。