WordPress: load full size image in thumbnail

2019-09-05 01:56发布

问题:

Because of using a magnifying glass script, I need to load the full image in the article (not featured image), even if the customer is choosing a thumbnail.

Example... this code should be generated:

<img src="..../uploads/image.png" width="300" height="500" />

and not

<img src="..../uploads/image-300x500.png" width="300" height="500" />

Anybody with a cool snippet for that? Thanks!

EDIT: I mean images that were used in the article, not the post/featured/thumbnail images-function.

回答1:

There are four valid sizes built in to the WordPress core.

the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)
the_post_thumbnail('full');            // Original image resolution (unmodified)

The last is one you're looking for.

The following returns the URL. With full size.

<?php if (has_post_thumbnail())
 $imageUrl =  wp_get_attachment_image_src(get_post_thumbnail_id(),'full');?>
<img alt="Post Thumbnail" src="<?php echo esc_url($imageUrl[0]); ?>">

for Blog Image you can use -

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img alt="Post Thumbnail" src="<?php echo esc_url($imageUrl[0]); ?>">
<?php endif; ?>

If you want to set image size in code you can use following code in function.php file

<?php add_image_size('product-size-large',300, 500, true);?>

then use this size here

<?php $imageUrl =  wp_get_attachment_image_src(get_post_thumbnail_id(),'product-size-large'); ?>

Also, for more options see the Codex.



回答2:

You can use the_post_thumbnail('full','true'); to get the original image size. OR You can also use

function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
    $post = get_post( $post );
    if ( ! $post ) {
        return '';
    }
    $post_thumbnail_id = get_post_thumbnail_id( $post );

in your function.php



回答3:

You can get image thumbnail first then can inset in image tag. And in "get_post_thumbnail_id" you can insert any size that you define in your code. thumbnail is already define size in your function.php.

      <?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');?>
      <img src="<?php echo esc_url($thumbnail['0']); ?>" alt="Post Thumbnail"/>


回答4:

here is code for custom image upload in wordpress

add_filter( 'image_size_names_choose', 'my_custom_sizes' );

function my_custom_sizes( $sizes ) {
    return array_merge( $sizes, array(
    'your-custom-size' => __( 'Your Custom Size Name' ),
    ) );
}

// Assuming your Media Library image has a post id of 24...
echo wp_get_attachment_image( 24, 'your-custom-size' );