2017-07-04 9 views
0

を通じて、私は... http://mightymente.org/projects/aarohievents/design/ギャラリーレイアウトの記事

のようなレイアウトに何かをしたいしたギャラリーを開発しています私はそれのほとんどを開発した。..コードを確認し、それをimpproveする方法の提案をお願いします。

マイコード:

$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(); 
+0

に印刷し($ I == 6)との場合は、この、ネストされましたコードは決して実行されません。それを外に出しなさい。 – Doomenik

+0

私はそれを取得しませんでした。 – varsha

+0

またはそれ以上の条件 'if($ i%3 == 0)'を使用します – ravisachaniya

答えて

0

これはあなたのactuallコードです:

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

だから、それは次のようになります。@ravisachaniyaに基づいて

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

とあなたも行くことができるコメントこのソリューションで

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

のために、それはまた、もし($ I == 3)の内側に第六ループ

echo "</div><div class='col-md-6 col-sm-6 col-xs-12 grid_w3'>";