设置 wordpress 文章的缩略图

文章目录

    文章缩略图在 wordpress 称之为 Featured Images, 也称为 Post Thumbnails。

    如果是手动新建一个主题,默认这项功能是没有开启的,需要在 functions.php 中启用

    function theme_setup() {
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 400, 250 );
    }
    add_action( 'after_setup_theme', 'theme_setup' );
    

    set_post_thumbnail_size

    用于设置缩略图的大小,第三个参数

    $crop (bool|array) (Optional) Whether to crop images to specified width and height or resize. An array can specify positioning of the crop area. Default value: false

    可以通过传递 array 控制剪裁的区域

    // 对图片进行缩放,生成缩略图
    set_post_thumbnail_size( 50, 50 );
    
    // 剪裁
    set_post_thumbnail_size( 50, 50, true );
    
    // 剪裁左上方区域
    set_post_thumbnail_size( 50, 50, array( 'top', 'left')  );
    
    // 剪裁中心区域
    set_post_thumbnail_size( 50, 50, array( 'center', 'center')  );
    

    参考

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式