在WordPress中给没有封面图的文章增加默认缩略图


/**
 * 增加默认的文章特色图
 */
add_filter( 'post_thumbnail_html', 'my_post_thumbnail_html' );
function my_post_thumbnail_html( $html ) {
    if ( empty( $html ) ) {
        $default_images = [
            get_theme_root_uri() . '/twentytwentythree/assets/images/default-featured-image-1.jpg',
            get_theme_root_uri() . '/twentytwentythree/assets/images/default-featured-image-2.jpg',
            get_theme_root_uri() . '/twentytwentythree/assets/images/default-featured-image-3.jpg'
        ];
        $index = mt_rand(0, count($default_images) - 1);
        $html = "<img src=\"{$default_images[$index]}\"/>";
    }
    return $html;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注