大方形 wordpress 帖子缩略图 [英] Big square wordpress post thumbnails

查看:36
本文介绍了大方形 wordpress 帖子缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何获得方形 wordpress 缩略图吗?

Does anybody know how to get square wordpress thumbnails?

如果我使用这个图像不是方形的

If I use this the images aren't square

<?php the_post_thumbnail( array(205,205) ); ?>

但是如果我这样做,它们是方形的

But if I do this, they are square

<?php the_post_thumbnail( array(135,135) ); ?>

我需要创建一个缩略图库,比如 300 x 300 方形图像.

I need to create a thumbnail gallery with let s say 300 x 300 square images.

推荐答案

您必须先创建自己的图片大小.这是通过 add_image_size() 函数完成的.

You have to create your own picture size first. This is done with the add_image_size() function.

你可以这样做:

if ( function_exists( 'add_theme_support' ) ) { 
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'square-large', 300, 300, true); // name, width, height, crop 
    add_filter('image_size_names_choose', 'my_image_sizes');
}

function my_image_sizes($sizes) {
    $addsizes = array(
        "square-large" => __( "Large square image")
    );
    $newsizes = array_merge($sizes, $addsizes);
    return $newsizes;
}

这会为您的主题添加对缩略图的支持(如果还没有的话).它将创建一个裁剪为 300x300 像素的新图像大小.第二个函数给出了更好的描述,并确保它会显示在媒体插入对话框中.

This will add the support for thumbnails to your theme if it hasn't it already. It will create a new image size with cropped 300x300 pixels. The second function gives it a nicer description and makes sure it will show up in the media insert dialogs.

然后就可以这样使用了.

Then you can use it like this.

<?php the_post_thumbnail( 'square-large' ); ?>

您可以在主题的 functions.php 中添加这些行.如果您想确保在主题更新时这些行不会被覆盖,我强烈建议您创建一个子主题,您可以在这里阅读如何这样做.

You can add these lines in the functions.php of your theme. If you want to make sure that these lines aren't overwritten when the theme gets updated I strongly suggest to create a child theme, you can read here how to do that.

这不会影响现有图像.您可以使用以下代码重新创建丢失的缩略图:

This will not affect existing images. You can recreate the missing thumbnails with this code:

include_once( ABSPATH . 'wp-admin/includes/image.php' );
function regenerate_all_attachment_sizes() {
    $args = array( 'post_type' => 'attachment', 'numberposts' => 100, 'post_status' => null, 'post_parent' => null, 'post_mime_type' => 'image' ); 
    $attachments = get_posts( $args );
    if ($attachments) {
        foreach ( $attachments as $post ) {
            $file = get_attached_file( $post->ID );
            wp_update_attachment_metadata( $post->ID, wp_generate_attachment_metadata( $post->ID, $file ) );
        }
    }       
}
regenerate_all_attachment_sizes();

这只需要运行一次.

这篇关于大方形 wordpress 帖子缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆