首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过帖子进行画廊布局

通过帖子进行画廊布局
EN

Stack Overflow用户
提问于 2017-07-04 13:57:39
回答 1查看 31关注 0票数 0

我正在开发一个画廊,其中我想要一个布局,如..http://mightymente.org/projects/aarohievents/design/

大部分都是我开发的..请审阅代码,并提出改进建议。

我的代码:

代码语言:javascript
复制
$the_query = new WP_Query("showposts=6&cat=1&orderby=asc");
$count = 1; 
while ( $the_query ->have_posts() ) {$the_query ->the_post(); $i++;
    $thumb_id = get_post_thumbnail_id();
    $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size',     
   false); 
    list($width, $height) = getimagesize($thumb_url[0]);  
    if($width == 800){ 
        $class='grid-1';
    } elseif($width==270) { 
        $class='sub_grid gallery_w3l';
    }   
    if ($i == 1){
        echo "<div class='col-md-6 col-sm-6 col-xs-12 grid_w3'>";
    }
?>

    <div class="<?php echo $class; ?>">
    <a class="cm-overlay" href="<?php echo $thumb_url[0];?>">
    <img src="<?php echo $thumb_url[0];?>" alt=" " class="img-responsive" />
    <div class="w3agile-text w3agile-text-small">
    <h5><?php the_title(); ?></h5>
    </div>
     </a> <?php //echo $i ; ?>
     </div>
<?php 
    $count++; 
    if ($i == 3){
        echo "</div><div class='col-md-6 col-sm-6 col-xs-12 grid_w3'>";}
        if ($i == 6){
            echo "</div>";$i=0;
        }
    }
    wp_reset_postdata();
EN

回答 1

Stack Overflow用户

发布于 2017-07-04 14:09:41

这是你的实际代码:

代码语言:javascript
复制
   if ($i == 3){
       //At this position $i has to be 3, if not it will never come to here
       echo "</div><div class='col-md-6 col-sm-6 col-xs-12 grid_w3'>";
       if ($i == 6){
          //So this line will never be executed cause $i will never be 6 here
          echo "</div>";$i=0;
       }
    }

所以它应该是:

代码语言:javascript
复制
    if ($i == 3){
       echo "</div><div class='col-md-6 col-sm-6 col-xs-12 grid_w3'>";
    }
    if ($i == 6){
       echo "</div>";$i=0;
    }

或使用elseIf

基于@ravisachaniya的评论,你也可以

代码语言:javascript
复制
if ($i % 3 == 0){
     //This way it divides $i with 3 and if the levtover is 0 it goes inside
     //Means 3, 6, 9, 12, 15 and so on
     echo "</div><div class='col-md-6 col-sm-6 col-xs-12 grid_w3'>";
     if ($i == 6){
          echo "</div>";$i=0;
     }
 }

使用此解决方案,它还将在第6个循环中打印

代码语言:javascript
复制
echo "</div><div class='col-md-6 col-sm-6 col-xs-12 grid_w3'>";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44897939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档