2015-09-06 156 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'中時,它不起作用。 –