我正在尝试使用WordPress的特色帖子功能在博客模板(而不是帖子或页面)上显示图像作为背景。
这是我在模板文件中使用的代码:
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<style type="text/css">body { background: url('<?php echo $image[0]; ?>') }</style>样式标记正在页面上呈现,但它没有获得图像源。
同样的代码(或者,它的一个非常相似的版本)可以在我的其他模板上工作-只是不能在博客页面上工作。
任何想法都将不胜感激。
谢谢
发布于 2013-12-19 05:47:39
查看get_the_ID()在博客页面上返回的内容。
因为get_the_ID()很可能不会返回任何内容/非post(请不要引用我的话),所以没有缩略图可供显示。
当它在博客页面上时,你想显示什么缩略图?
===添加了===
如果你想显示第一篇博客文章的特色图片作为背景。
您必须将代码放入循环中
然而,因为它是背景图像,你只需要你的代码一次。
你能做的就是放入增量变量,这样你就可以知道循环的索引
<?php $index = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
if($index == 0) {
*** your code comes here ***
}
$index++;
endwhile;
endif; ?>https://stackoverflow.com/questions/20669041
复制相似问题