Wordpress自定义字段- css in循环


Wordpress custom field - css in loop

我尝试在背景图像属性中插入自定义字段的值(因此不在img src="..."') .

In my category.php;我可以显示链接到每个帖子的自定义字段;但是当我把变量放在我的样式(inline 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/

你的代码应该是这样的:

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

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

欢呼