the_post_thumbnail返回超链接外部



当我使用get_the_post_thumbnail时,它会在<a></a>内返回特色图像默认大小,但是当我使用它而不在函数文件中插入预定义的大小名称时,它会返回所需的大小,但在超链接之外。

<?php
$args = array('showposts' => 25);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ): 
echo '<ul>';
while ( $the_query->have_posts()) : $the_query->the_post();     
echo '<span><li><a href="'.get_the_permalink().'">' .the_post_thumbnail('shapely-grid').' '.get_the_title().'</a> <p>' .get_the_excerpt($limit).'</p></li></span>';
endwhile; 
echo '</ul>';
endif; 
wp_reset_query(); ?>

如果您阅读此处的文档 https://developer.wordpress.org/reference/functions/the_post_thumbnail/您会发现该函数会立即执行"echo",就像大多数以"the_"开头的 wp 函数一样。 因此,要么使用 https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/,要么将代码更改为以下内容:

echo '<span><li><a href="'.get_the_permalink().'">' ;
the_post_thumbnail('shapely-grid');
echo ' '.get_the_title().'</a> <p>' .get_the_excerpt($limit).'</p></li></span>';

最新更新