wordpress自定义post div不正确的循环

时间:2016-02-04 14:03:31

标签: wordpress

我是wp的新手,并且很少在div中使用自定义post循环,这里是我的代码可以有人构造我在div上。只有3个帖子应该在signle行中,但journey_gallery_content只会重复,journey_gallery_row 不要重复。

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

2 个答案:

答案 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>

尝试这样的事情。

答案 1 :(得分:0)

使用current_post支票

这应该可以将每3个帖子放入the journey_gallery_row div

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