2014-06-20 36 views
-1

這是我創建列後的代碼列,我想在3列之後分列。 (我是新來的wp代碼)。我想要在Wordpress foreach循環中有三列

<table> 
    <tr> 
     <?php 
      $posts = get_posts('category=3&numberposts=10'); 
      foreach($posts as $post) { ?> 
       <td width="150"> 
        <?php if (has_post_thumbnail()) { ?> 
         <a target="_parent"> <?php the_post_thumbnail(array(200,100)); ?></a> 
        }?> 
        <a target="_parent"><?php the_title(); ?></a> 
        <?php echo get_post_meta(get_the_ID(), 'event_startEvent Start', true); ?> 
       </td> 

     <?php } ?> 
    </tr> 
</table> 

回答

0

您可以通過改變這部分代碼實現這個

<?php 
    $posts = get_posts('category=3&numberposts=10'); 
     foreach($posts as $post) { 
?> 

<?php 
     $count = 1; 
     $posts = get_posts('category=3&numberposts=10'); 
     foreach($posts as $post) { 

     if($count == 4){ 
      echo '</tr><tr>'; 
      $count = 0; 
     } 
     $count++; 

?> 
+0

這很好用,謝謝 –

+0

@ user3759400請標記答案爲接受以關閉您的問題。 – arielnmz

0

試試這個:

<table> 
    <?php 
     $posts = get_posts('category=3&numberposts=10'); 
     $colCount = 0; 
     foreach($posts as $post) { 
      if ($colCount == 0) echo '<tr>'; 
      echo ' 
       <td width="150"> 
        <a target="_parent"> 
         '.(has_post_thumbnail() ? /* check if the post has a Post Thumbnail assigned to it. */ 
          set_post_thumbnail_size(200, 100) : 
          the_post_thumbnail()).' 
        </a> 
        <a target="_parent">'. the_title() .'</a> 
        '.get_post_meta(get_the_ID(), 'event_startEvent Start', true).' 
       </td>'; 
      $colCount++; 
      if ($colCount > 2) { 
       echo '</tr>'; 
       $colCount = 0; 
      } 
     } 
    ?> 
</table> 

最好是在一個單一的串聯HTML php標籤(只要有可能),比打開和關閉你需要執行的每一小段代碼的php標籤。

而且,你的HTML被打破:

  1. <a>標籤應該是這樣的:

    <a target="_parent"> 
    
0

我知道這可能不是回答你的問題,因爲你真的想使用表,但你有沒有考慮清單?

<ul id="theList"> 
    <li><img /><a href="#">The title</a></li> 
    <li><img /><a href="#">The title</a></li> 
    <li><img /><a href="#">The title</a></li> 
    <li><img /><a href="#">The title</a></li> 
    <li><img /><a href="#">The title</a></li> 
    <li><img /><a href="#">The title</a></li> 
    <li><img /><a href="#">The title</a></li> 
</ul> 

<style> 
#theList { 
    margin:0; 
    padding: 0; 
    list-style: none; 
    width: 450px; // more if <li> has padding 
} 
#theList li { 
    float: left; 
    width: 150px; 
} 
</style>