0

我正在尝试将我的 HTML 页面转换为 WordPress 主题,到目前为止我做得还不错!但我在加载自定义帖子类型缩略图并向其添加所需的类和样式时遇到问题。在我的 HTML 中,我有一个像这样的图像标签:

<img class="img-circle" src="img/welcom.jpg" data-src="holder.js/140x140" alt="140x140" style="width: 140px; height: 140px;"> 

我通过污染 src 属性来在我的自定义 WP 查询中回显上面的标签,例如src="'.the_post_thumbnail().'"

echo ' <img class="img-circle" src="'.the_post_thumbnail().'" alt="140x140" style="width: 140px; height: 140px;">';

现在我在页面上获取图像,但没有获取任何类和样式,源代码看起来像 在此处输入图像描述 你能告诉我如何解决这个问题吗?

4

2 回答 2

2

the_post_thumbnail()您返回 src url 的假设是错误的。它返回整个 img 元素...看看codex

但本质上你需要将类、大小和 alt 作为参数传递给the_post_thumbnail()

$size = array(140,140);
$attr = array(
    'class' => "img-circle",
    'alt'   => "140x140"
);
the_post_thumbnail( $size, $attr );
于 2013-11-01T01:30:32.283 回答
1

尝试使用此代码,它将帮助您处理所有其他图像......

将此代码放入您的function.php文件中

if ( function_exists( 'add_image_size' ) ) { 
    add_image_size( 'custom-post-thumb', 140, 140,true );
}

现在在您的代码中使用它:

<?php if ( has_post_thumbnail()) : ?>

 <?php the_post_thumbnail('custom-post-thumb', array('class' => 'img-circle')); ?>

<?php endif; ?>

谢谢

于 2013-11-01T05:49:24.810 回答