我尝试在background-image属性中插入自定义字段的值(因此不在img src="..."')
中。
在我category.php
;我可以显示链接到每个帖子的自定义字段;但当我将变量放入样式(内联css)时,wordpress始终显示相同的图像。
代码:
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<style type="text/css">
.photo_interview {
background-image: url(<?php echo $photo_interview; ?>);
}
</style>
<div class="photo_interview"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
有什么想法吗?我的页面在这里:http://www.overso.me/category/interview/
答案 0 :(得分:2)
您的代码应为:
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<!-- You don't need style tags if you only want to set the background image -->
<div style="background-image: url(<?php echo $photo_interview; ?>)"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
答案 1 :(得分:1)
我可以看到你没有设置全局$post
,所以get_field不知道需要显示哪个。在这种情况下,最好使用the_ID()
来获取while循环中的当前帖子ID。
干杯