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) ); ?>
I need to create a thumbnail gallery with let s say 300 x 300 square images.
You have to create your own picture size first. This is done with the add_image_size() function.
You can do it like this:
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;
}
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' ); ?>
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();
This only has to be run once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With