WordPress:在缩略图中加载完整大小的图像

时间:2016-07-11 06:43:07

标签: php wordpress image

由于使用放大镜脚本,我需要在文章(非特色图片)中加载完整图像,即使客户选择缩略图也是如此。

示例...应生成此代码:

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

而不是

<img src="http://dummyimage.com/200x200/000/ffffff&text=Light+Switch+Off" class="lighSwitchOff" /><br>
<img src="http://dummyimage.com/200x200/000/ffffff&text=Stick+Man" class="stickman" />

有人为此提供了很酷的片段吗?谢谢!

编辑:我的意思是文章中使用的图像,而不是帖子/特色/缩略图图像功能。

4 个答案:

答案 0 :(得分:1)

WordPress核心内置了四种有效的大小。

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)

最后一个是你正在寻找的。

以下内容返回URL。 完整尺寸

<?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]); ?>">

博客图片,您可以使用 -

<?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; ?>

如果您想在代码中设置图片大小,您可以在function.php文件中使用以下代码

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

然后在这里使用这个尺寸

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

此外,有关更多选项,请参阅Codex

答案 1 :(得分:0)

你可以使用the_post_thumbnail('full','true'); 获得原始图像大小。         要么 您也可以使用

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 );

在你的function.php中

答案 2 :(得分:0)

您可以先获取图像缩略图,然后插入图像标签。在&#34; get_post_thumbnail_id&#34;您可以插入在代码中定义的任何大小。 thumbnail已经在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 :(得分:0)

这是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' );
相关问题