2010-03-28 24 views
1

我想在兩個div的類別中顯示我的wordpress帖子。因此,例如:將WordPress查詢帖轉換爲兩個div

<ul id="left"> 

<li class="post">POST 1</li> 

<li class="post">POST 3</li> 

<li class="post">POST 5</li> 

<li class="post">POST 7</li> 

</ul> 

<ul id="right"> 

<li class="post">POST 2</li> 

<li class="post">POST 4</li> 

<li class="post">POST 6</li> 

<li class="post">POST 8</li> 

</ul> 

所以想我需要做的是告訴query_posts以某種方式開始每個DIV奇怪,然後吐出第4柱均勻。我不想有兩個獨立的WP_Queries,因爲這是一個category.php文件,需要有默認循環。不太確定如何做到這一點。

任何幫助非常讚賞。

回答

1

如何預先建構的兩個列表:(我不記得WP查詢語法,所以它的僞PHP :)

<?php 
$list1 = array(); 
$list2 = array(); 
$i=0; 
foreach($query_results as $res) { 
    if(($i++)&1) $list2[] = $res; 
    else $list1[] = $res; 
} 
?> 

現在列表1包含第一,第三,......項目和List2包含第二個,第四個,... 然後,您可以隨意在兩個div中打印它們。

(在切線:是否PHP有任何簡潔的方式來做到上面的代碼做什麼Python有階梯切片語法...)

+0

我沒有在這個跌跌:http://perishablepress.com/press/2008/08/04/two-column-horizo​​ntal-sequence-wordpress-post順序/但我正在尋找一個更簡單的方法來做到這一點。另外,它不會像上面顯示的那樣完全一樣。 – Cameron 2010-03-28 12:17:05

0

如果你的目標是有職位的兩列列表,它是隻輸出一個列表中的職位要容易得多,然後用CSS來給使用浮動兩列的視覺外觀,例如:

width: 50%; 
float: left; 
2

我以前沒有考這個,這是不最好的辦法,但解決方案

<?php if (have_posts()) : ?> 

    <?php while (have_posts()) : the_post(); ?> 

     <?php 
      $count++; 

      if($count % 2) { 
       $left_array[] = array('content' => get_the_content('more...')); 
      } 
      else { 
       $right_array[] = array('content' => get_the_content('more...')); 
      } 
     ?> 

    <?php endwhile; ?> 

    <ul id="left"> 
     <?php 
     foreach($left_array as $post) { 
      echo '<li class="post">'.$post['content'].'</li>'; 
     } 
     ?> 
    </ul> 
    <ul id="right"> 
     <?php 
     foreach($right_array as $post) { 
      echo '<li class="post">'.$post['content'].'</li>'; 
     } 
     ?> 
    </ul> 

<?php else : ?> 

<?php endif; ?> 

或同樣的想法,而是另闢蹊徑:

<?php if (have_posts()) : ?> 

    <?php while (have_posts()) : the_post(); ?> 

     <ul id="left"> 
     <?php 
      $count++; 

      if($count % 2) { 
      } 
      else { 
     ?> 
       <li class="post"><?php the_content('more...'); ?></li> 
     <?php 
      } 
     ?> 
     </ul> 

     <ul id="right"> 
     <?php 
      $count++; 
      if($count % 2) { 
     ?> 
       <li class="post"><?php the_content('more...'); ?></li> 
     <?php 
      } 
     ?> 
     </ul> 

    <?php endwhile; ?> 

<?php else : ?> 

<?php endif; ?> 
+0

頂端的作品,但吐出原始內容,而不是你通常得到的格式化內容(P標籤等)。最下面的一個不起作用。任何想法爲什麼?謝謝。 – Cameron 2010-03-28 18:03:50

+0

啊是的,第二個在我的想法中有一個錯誤 - 我會嘗試解決這個問題 – ahmet2106 2010-03-29 18:40:56