1

How to display thumbnail wordpress in bootstrap popover?

I used the_post_thumbnail but this function inherently echo <img> . The resulting image is not shown in popover

<?php 
if ( have_posts() ) : while ( have_posts() ) : the_post();

/*********display the_post_thumbnail in data-content of popover *********/

echo '<a href="'.get_permalink().'" rel="popover" data-title="'.get_the_title().'" data-content="'.the_post_thumbnail('full').'">';


the_title();
echo '</a>';
endwhile; endif;
wp_reset_query();
?>
4

1 回答 1

1

正如你所说,the_post_thumbnail()固有地回显整个<img>标签,所以当你回显它时它会做意想不到的事情。改为这样做:

echo '<a href="'.get_permalink().'" rel="popover" data-title="'.get_the_title().'" data-content="';
the_post_thumbnail('full');
echo '">';

您现在很有可能会遇到 Wordpress 为您提供的元素中未转义的双引号的问题<img>,因此仅获取缩略图 URL 可能更有意义:

$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
$url = $thumb['0'];


echo '<a href="'.get_permalink().'" rel="popover" data-title="'.get_the_title().'" data-content="<img src=\''.$url.'\'>">';
于 2013-07-29T22:28:11.983 回答