-2

我是 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 }
      ?>

谢谢!

4

1 回答 1

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

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

于 2013-12-24T16:49:02.183 回答