如何在 Wordpress 中动态获取 link 到缩略图

How to get dynamically link to thumbnail in Wordpress

如何使用 WordPress 函数获取图片缩略图 Url 而不是 href="img/1.jpg..

<a class="popup-link" href="img/1.jpg"></a>

您可以使用get_the_post_thumbnail功能。

<a class="popup-link" href="<?php echo get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' );"></a>

参数

$post_id (int)(可选)Post ID。默认是 $post 全局的 ID。 默认值:空

$尺码 (string|array)(可选)要使用的注册图像大小,或高度和宽度值的平面数组。 默认值:'post-thumbnail'

$attr *(string|array)(可选)查询字符串或属性数组。

默认值:''*

详情请访问 https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

您可以使用 WordPress 功能获取图像缩略图 Url..

<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?> 

使用示例:

<?php
$imgarray = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'full');
$imgURL = $imgarray[0];
?>
<a class="popup-link" href="<?php echo $imgURL;?>"></a>

参考抄本

https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

看看函数<?php the_post_thumbnail( $size, $attr ); ?> 这个 $size and $attr 是 2 个参数和 $size is string/array 图片大小。字符串关键字(缩略图、中等、大、完整)或定义的任何自定义大小关键字。 $attr is array 对 attribute/value 对 Default: None 让它在其中工作 $default_attr = array( 'src' => $src, 'class' => "attachment-$size", 'alt' => trim( strip_tags( $wp_postmeta->_wp_attachment_image_alt ) ), 'title' => trim( strip_tags( $attachment->post_title ) ) );
如果你只是在没有参数 the_post_thumbnail(); 的情况下使用它,那么它会给出默认大小 post_thumbnail 和
如果你 the_post_thumbnail('large' ); 那么你可以看到大尺寸的图像,你可以通过 WordPress Administration Media panel under Settings > Media
更改这两个尺寸 现在如果你 <?php the_post_thumbnail( 'anysize', array( 'class' => 'popup-link' ) ); ?>
或者您可以使用 wp_get_attachment_image_src( $attachment_id, $size, $icon ); this functions for getting image URL and then $imgURL = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'full'); <a class="popup-link" href="<?php echo $imgURL[0];?>"></a>