2016-02-04 52 views
0

我是新来的WP和少量与自定义后循环内部div,这里是我的代码可以有人结构我砍在div。只有3个帖子应该在signle行中,但是journey_gallery_content只会重复,journey_gallery_row 不会重复。WordPress的自定义帖子div不正确的循环

<div class="journey_gallery_row"> 
     <?php 
       $loop=new WP_Query(array('post_type'=>'journeypackage', 
              'sort_column' => 'post_date', 
              'posts_per_page'=> -1 , 
              'order' => 'ASC') 
            ); 
       if ($loop->have_posts()) 
        { 
         while ($loop->have_posts()) 
          { 
         $loop->the_post(); 
         $meta=get_post_meta(get_the_id(),''); 
       ?> 
      <div class="journey_gallery_content"> 
        <?php echo get_the_post_thumbnail(); ?> 
       <div class="journey_package"> 
        <div class="journey_package_img"> 
         <?php 
        $attachment_id = get_field('package-icon'); 
        $size = "et-member-image-small"; 

        $image = wp_get_attachment_image_src($attachment_id, $size); 
        ?> 
        <img src="<?php echo $image[0]; ?>" /> 
        </div> 
        <div class="journey_package_name"> 
         <a href="<?php bloginfo('url')?>/journeytitle/enture/"><?php echo get_the_title(); ?></a> 
         <p class ="">Date : <?php the_field('package-date'); ?> </p> 

        </div> 

       </div> 

      </div> 
      <?php } 
      }?> 
      </div> 
     </div> 
    </div> 
</div> 

回答

0

使用current_post检查

这应该让每个3个员额投入the journey_gallery_row

<?php 
       $loop=new WP_Query(array('post_type'=>'journeypackage', 
              'sort_column' => 'post_date', 
              'posts_per_page'=> -1 , 
              'order' => 'ASC') 
            ); 
       if ($loop->have_posts()){?> 
        <div class="journey_gallery_row"> 
         <?php 
         while ($loop->have_posts()) 
          { 
         $loop->the_post(); 
         $meta=get_post_meta(get_the_id(),''); 

       ?> 


          <div class="journey_gallery_content"> 
        <?php echo get_the_post_thumbnail(); ?> 
       <div class="journey_package"> 
        <div class="journey_package_img"> 
         <?php 
        $attachment_id = get_field('package-icon'); 
        $size = "et-member-image-small"; 

        $image = wp_get_attachment_image_src($attachment_id, $size); 
        ?> 
        <img src="<?php echo $image[0]; ?>" /> 
        </div> 
        <div class="journey_package_name"> 
         <a href="<?php bloginfo('url')?>/journeytitle/enture/"><?php echo get_the_title(); ?></a> 
         <p class ="">Date : <?php the_field('package-date'); ?> </p> 

        </div> 

       </div> 
</div> 
      <?php if($loop->current_post % 3 == 2) echo '</div>'."\n".'<div class="journey_gallery_row">'; ?> 
      <?php } 
      }?> 
+0

我用你的答案得到了确切的结果。 –

0

我认为你需要在与循环的结束一个变量,增量环添加自定义的检查,该变量会检查是否等于行限制的限制则反而会加重关闭旧的行div并在内容div之前添加新的行div。可能是这样的。

<div class="journey_gallery_row"> 
    <?php 
      $loop=new WP_Query(array('post_type'=>'journeypackage', 
             'sort_column' => 'post_date', 
             'posts_per_page'=> -1 , 
             'order' => 'ASC') 
           ); 
      if ($loop->have_posts()) 
       { 
       $num = 1; 
        while ($loop->have_posts()) 
         { 
        $loop->the_post(); 
        $meta=get_post_meta(get_the_id(),''); 
        if($num%4==0){echo '</div><div class="journey_gallery_row">';} 
      ?> 

     <div class="journey_gallery_content"> 
       <?php echo get_the_post_thumbnail(); ?> 
      <div class="journey_package"> 
       <div class="journey_package_img"> 
        <?php 
       $attachment_id = get_field('package-icon'); 
       $size = "et-member-image-small"; 

       $image = wp_get_attachment_image_src($attachment_id, $size); 
       ?> 
       <img src="<?php echo $image[0]; ?>" /> 
       </div> 
       <div class="journey_package_name"> 
        <a href="<?php bloginfo('url')?>/journeytitle/enture/"><?php echo get_the_title(); ?></a> 
        <p class ="">Date : <?php the_field('package-date'); ?> </p> 

       </div> 

      </div> 

     </div> 
     <?php $num++; 
     } 
     }?> 
     </div> 
    </div> 
</div> 

尝试这样的事情。

+0

由于作品般的魅力,有一个愉快的一天。 另外我有日期选择器自定义字段 日期:20160203另一个问题,日期被格式化为这样,它没有得到改变。我正在使用高级自定义字段,我希望日期格式为Date:2016/02/03 –

+0

可能会在plugin中设置日期格式化的一些设置。如果没有,则可能是插件的任何钩子。 –