WordPress: load full size image in thumbnail

2019-09-05 01:38发布

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.

4条回答
戒情不戒烟
2楼-- · 2019-09-05 01:39

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"/>
查看更多
地球回转人心会变
3楼-- · 2019-09-05 01:48

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' );
查看更多
干净又极端
4楼-- · 2019-09-05 01:52

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.

查看更多
Animai°情兽
5楼-- · 2019-09-05 02:04

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

查看更多
登录 后发表回答