2015-09-06 66 views
1

我试图插入自定义字段的值,在背景图片属性(因此不img src="..."')WordPress的自定义字段 - CSS中环

在我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; ?> 

任何想法,我的页面在这里:?

回答

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

对于我所看到的你没有设置全局$post,所以get_field不知道需要显示哪个。在这种情况下,最好使用the_ID()来获取while循环中的当前帖子ID。

干杯

+0

我用'the_ID()代替'$后> ID'。我不明白为什么'echo $ photo_interview;'显示良好的url;但是当我把变量放在'background-image'中时,它不起作用。 –