2

我正在尝试创建一个包含两个 div 的滑块,然后关闭。现在我有一个计数器,如果帖子可以被两个整除,它将关闭并打开一个新的 div。不幸的是,它也在打开和关闭一个空的 div。这是我的代码:

   <div id="brew-slider" class="global-width cycle-slideshow" data-cycle-fx="fade" data-cycle-timeout="0" data-cycle-slides="> div" data-cycle-pager=".cycle-pager" data-cycle-auto-height="container">
<?php
  $args = array(
    'post_type' => 'beer',
  );
  $beer_list = new WP_Query( $args );
  $post_counter = 1;
  if($beer_list->have_posts()){
  ?>
  <div class="brew-slide group">
  <?php
    while($beer_list->have_posts()) {
      $beer_list->the_post();
      $hops = get_post_meta($post->ID, 'hops', true);
      $url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
      $abv = get_post_meta($post->ID, 'abv', true);
      $ibu = get_post_meta($post->ID, 'ibu', true);
      $availability = get_post_meta($post->ID, 'availability', true);
  ?>
    <div class="col2-slide floatleft group">
      <img src="<?=$url[0]?>" alt="<?php the_title(); ?>">
      <div class="brew-content">
        <h3 class="brew-title"><?php the_title(); ?></h3>
        <?php the_tags(); ?>
        <?php the_content();?>
        <p><strong>Hops:</strong> <?=$hops?></p>
        <h3 class="green-box">ABV: <?=$abv?>%</h3>
        <h3 class="green-box">IBU's: <?=$ibv?></h3>
        <h4 class="green-box"><?=$availability?></h4>
     </div>
    </div>
 <?php
     if($post_counter % 2 == 0) {echo '</div><div class="brew-slide group">';}
     $post_counter++;
  }//End While Loop
 ?>
  </div>
 <?php
  }//End of If
  else {
 ?>
   <p>Currently no beers listed</p>
 <?php
  }
  wp_reset_postdata();
?>
</div>
4

2 回答 2

0

您应该检查是否$post_counter是偶数这不是最后一篇文章。像这样:

if($post_counter % 2 == 0 && $beer_list->have_posts()) 
  {echo '</div><div class="brew-slide group">';}
于 2013-11-04T17:14:43.283 回答
0

所以经过一些研究,我能够自己得到它。感谢所有试图提供帮助的人。这是我所做的答案。

                            <?php
                                $args = array(
                                    'post_type' => 'beer',
                                );
                                $beer_list = new WP_Query( $args );
                                if($beer_list->have_posts()){
                                ?>
                            <?php
                                    while($beer_list->have_posts()) {
                                        $beer_list->the_post();
                                        $hops = get_post_meta($post->ID, 'hops', true);
                                        $url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
                                        $abv = get_post_meta($post->ID, 'abv', true);
                                        $ibu = get_post_meta($post->ID, 'ibu', true);
                                        $availability = get_post_meta($post->ID, 'availability', true);
                                        if($beer_list->current_post%2 == 0) echo "\n".'<div class="brew-slide group">'."\n";
                                    ?>
                            <div class="col2-slide floatleft group">
                                   <img src="<?=$url[0]?>" alt="<?php the_title(); ?>">
                                <div class="brew-content">
                                    <h3 class="brew-title"><?php the_title(); ?></h3>
                                    <?php the_tags(); ?>
                                    <?php the_content();?>
                                    <p><strong>Hops:</strong> <?=$hops?></p>
                                        <h3 class="green-box">ABV: <?=$abv?>%</h3>
                                        <h3 class="green-box">IBU's: <?=$ibv?></h3>
                                        <h4 class="green-box"><?=$availability?></h4>
                                </div>
                            </div>
                                <?php
                                        if($beer_list->current_post%2 == 1 || $beer_list->current_post == $beer_list->post_count-1) echo '</div>'."\n";
                                    }
                                ?>
                                <?php
                                }
                                else {
                                ?>
                                <p>Currently no beers listed</p>
                            <?php
                                }
                                wp_reset_postdata();
                            ?>
于 2013-11-07T22:19:17.507 回答