为什么这个Wordpress循环只显示最后一张图像?

时间:2013-12-24 16:45:07

标签: php wordpress loops if-statement advanced-custom-fields

我是Wordpress的新手,我想知道为什么这个循环只显示最后一张图片而不是所有图片。任何参考?这里也是链接:http://87cen.com/desmob/

<?php get_template_part('templates/page', 'header'); ?>
<?php get_template_part('templates/content', 'page'); ?>

<?php $temp_query = $wp_query; ?>
  <?php query_posts('showposts=10'); ?>
    <?php while (have_posts()) : the_post(); ?>
      <div class="post" id="post-<?php the_ID(); ?>">
        <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h4>
      </div>
      <?php endwhile; ?>

    <?php
      $img = get_field('image');
      if($img) { ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo get_field('image'); ?>"></a>
      <?php }
      ?>

谢谢!

1 个答案:

答案 0 :(得分:1)

您的逻辑中存在轻微问题:

<?php while (have_posts()) : the_post(); ?>
  <div class="post" id="post-<?php the_ID(); ?>">
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h4>
  </div>
  <?php endwhile; ?>

你的循环在这一点结束;但你现在去打印图像。您将使用上次迭代中的图像。您需要做的就是重新安排循环:

<?php while (have_posts()) : the_post(); ?>
  <div class="post" id="post-<?php the_ID(); ?>">
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h4>
  </div>

  <?php 
  $img = get_field('image');
  if($img) { ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo get_field('image'); ?>"></a>
  <?php }
  ?>

  <?php endwhile; ?>

图像代码现在位于循环内部,并且应该在每次迭代时显示。