2012-05-23 160 views
1

這是我擁有的標準HTML代碼,我試圖將它合併到一個基本上以兩種不同方式顯示帖子的Wordpress循環中。我使用更多的字段插件爲用戶選擇顯示帖子的兩種方式之一。 1)如果他們選擇Large,那麼帖子會被div類的「大鏈接」所包裹。2)如果用戶選擇「Small」,則會創建一個類爲「groupOflinks」的div,並且該帖子被div類「smallLink 」。只有4個'smallLink'div/posts可以放在一個'groupOflinks'div內。如果有超過4個小帖子,則創建一個新的「groupOflinks」div,並重復該過程。Wordpress嵌套循環

下面是HTML代碼我試圖合併(大量註釋):

<!-- Display one post within this container only & if there are more posts that have been chosen as Large, wrap them in the 'largeLink' div --> 

<div class="largeLink"> 
    <!-- post 1 content here --> 
</div> 

<div class="largeLink"> 
    <!-- post 2 content here --> 
</div> 

<!-- If a post is defined as small within the admin panel (using more fields plugin) 
    display them within this 'groupoflinks' container 
    If 4 posts are already in this 'groupoflinks' container create a new container and populate with another 4 posts 
    Repeat --> 

<div class="groupOfLinks"> 
    <!-- This is a container that holds 4 posts only & if there are more posts that have been assigned as 'Small' create a new 'groupOflinks' container and populate with the next 4 --> 
    <div class="smallLink"> 
     <!-- Post 4 content here --> 
    </div> 
    <div class="smallLink"> 
     <!-- Post 5 content here --> 
    </div> 
    <div class="smallLink"> 
     <!-- Post 6 content here --> 
    </div> 
    <div class="smallLink"> 
     <!-- Post 7 content here --> 
    </div> 
</div> 

<div class="largeLink"> 
    <!-- post 3 content here --> 
</div> 

<div class="largeLink"> 
    <!-- post 8 content here --> 
</div> 

這是我的WP循環到目前爲止這完全不是那麼回事:

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

<?php // Take the value defined with the 'Layout' field and change style accordingly 
$layouttype = get_post_meta($post->ID, 'layout-type', true) ?> 
<?php if ($layouttype == 'Small') { ?> 

<div class="groupOfLinks"> 

    <!--LOOP STYLE 1 GOES HERE--> 
    <?php $temp_query = $wp_query; // store it 
     $args = array(
     'paged' => $paged, // paginates 
     'post_type'=>'post', 
     'order' => 'DESC' 
     ); 
     $wp_query = new WP_Query($args); 
     while ($wp_query->have_posts()) : $wp_query->the_post();?> 

     <div class="smallLink"> 
         <!-- Post content here --> 
         <h1><?php the_title(); ?></h1> 
      <?php the_content(''); ?> 
        </div> 


     <?php if (isset($wp_query)) {$wp_query = $temp_query;} // restore loop 
       endwhile; ?> 

</div> 

<?php } else { ?> 

<!--LOOP STYLE 2 GOES HERE--> 

<div class="largeLink"> 
     <!-- post 1 content here --> 
     <h1><?php the_title(); ?></h1> 
     <?php the_content(''); ?> 
    </div> 


<?php } ?> 


<?php endwhile; ?> 

<div> 
<div><?php next_posts_link('&laquo; Older Entries') ?></div> 
<div><?php previous_posts_link('Newer Entries &raquo;') ?></div> 
</div> 

<?php else : ?> 

<h2>Not Found</h2> 
<p>Sorry, but you are looking for something that isn’t here.</p> 

<?php endif; ?> 
+0

只是爲了澄清,你想要兩個大帖子,然後是一組4小帖子,然後重複,直到你完成你的帖子? –

+0

你想要它的任何方式,我有一個解決方案,只是澄清最終目標。 –

+0

他們進來的順序並不重要,儘管最好按照日期順序。最新的帖子第一,最舊的帖子最後。因此,根據他們添加的日期,我可以首先顯示一個4個郵政集裝箱,然後是5個(例如)大型單個職位,然後是另一個小型郵政集裝箱。或者我可以有一個大的單一職位,然後是3個小型郵政集裝箱。我希望它在外觀上有所不同,取決於每篇文章的發佈時間。我希望這是有道理:) – egr103

回答

2

我有在幾分鐘內召開一次會議,所以爲了時間的緣故,我會以正確的方向開始你的工作,你可能不得不做一些調整,因爲我寫這個很匆忙。讓我們從你的循環開始,做一些快速的改變。

<?php 
//Set a counter to determine if small post has been used 
$small_posts = 0; 
if(have_posts()) : 
    while(have_posts()): the_post(); 
    $layouttype = get_post_meta(get_the_ID(), 'layout-type', true); 
    if($layouttype == 'Small') : 
     if(($small_posts % 4) == 0) : 
      $tmp_query = clone $wp_query; ?>//this line was edited 
      <div class="groupOfLinks"> 
       <div class="smallLink"> 
        <h1><?php the_title(); ?></h1> 
        <?php the_content(); ?> 
       </div> 
       <?php $i = 1; 
       while(have_posts() && $i <=4) : the_post(); 
        $layouttype = get_post_meta(get_the_ID(), 'layout-type', true); 
        if($layouttype == 'Small') : ?> 
         <div class="smallLink"> 
          <h1><?php the_title(); ?></h1> 
          <?php the_content(); ?> 
         </div> 
         <?php $i++; 
        endif; 
       endwhile; ?> 
       </div> 
       <?php $wp_query = clone $tmp_query;//this line was edited 
     endif;//End counter check 
     //Whether we needed to enter the subloop or not, the counter is incremented 
     $small_posts++; 
    else: //Else for if($layouttype == 'Small') ?> 
     <div class="largeLink"> 
      <h1><?php the_title(); ?></h1> 
      <?php the_content(); ?> 
     </div> 
    <?php endif; 
    endwhile; ?> 
<div> 
    <div><?php next_posts_link('&laquo; Older Entries') ?></div> 
    <div><?php previous_posts_link('Newer Entries &raquo;') ?></div> 
</div> 
<?php else : ?> 
    <h2>Not Found</h2> 
    <p>Sorry, but you are looking for something that isn’t here.</p> 
<?php endif; ?> 
+0

謝謝你,唯一的問題,但它是打破我的網頁:(試圖尋找任何次要的字符errores等,但不能看到任何明顯的... – egr103

+0

我會再次看看,一旦我離開我的會議 –

+0

@ egr103我現在修復了它,它丟失了一些分號,我測試了它,它的確如預期的那樣工作了, –