2011-12-06 30 views
0

這是一些基本的IF THEN聲明的東西,但我是一個編碼新手,所以我希望有人可以扔我骨頭:-)。如何返回單個結果,如果所有帖子在循環匹配條件

我有幾個職位有自定義日期字段。我想:

1)遍歷所有這些帖子,並評估每個帖子的日期字段是否過去。如果是這樣,我想顯示特殊消息

2)如果只是其中一個帖子在過去,我想繼續評估,直到查詢中所有帖子的末尾都到達。 3)如果發現即使有一個日期爲將來的帖子,我也會從該帖子中顯示更多內容,而不是專用帖子

當前,特殊消息僅當循環中的項目根本沒有發佈內容時才顯示。在評估每篇文章之後,我也能夠得到它,但之後我對每篇評論文章都收到了特別留言,我甚至希望即使在任何一個帖子的任何一個帖子都會返回true。

我當前的代碼是這樣的:

     <?php 

         $parent = get_cat_name($category[0]->category_parent); 
         $cur_cat = $cur_cat_slug; 
         $cur_date = current_time('timestamp',0); 

         echo $cur_cat_name; 

         ?> 
        </div> 

        <div class=dates> 
         <?php 

         $categoryvariable=$category; // assign the variable as current category 
         $query= 'cat=' . $cur_cat_id. '&posts_per_page=100&meta_key=date_value&orderby=meta_value&order=ASC&meta_compare=>=&meta_value=$cur_date'; // concatenate the query 
         query_posts($query); // run the query 
         if (have_posts()) : while (have_posts()) : the_post(); 
         $date_value = get_post_meta($post->ID, 'date_value', true); 
         if ($date_value>=$cur_date) 
          { 
         ?> 
          <a class=dates-link href="<?php the_permalink(); ?>"> 
          <li><?php echo date("D, n/j/Y, g:ia", get_post_meta($post->ID, 'date_value', true)); ?> - <?php $key="course_endtime"; echo get_post_meta($post->ID, $key, true); ?> 
          </a> 
         <div class=info> 
          <table cellpadding=0 cellspacing=3 border=0> 
           <tr> 
            <td valign=top><img src="<?php bloginfo('template_directory'); ?>/images/i.jpg"></td> 
            <td>Click any date for more info about a course and to register online.</td> 
           </tr> 
          </table> 
         </div> 
         <?php 
          } 

         endwhile; else: 

         ?> 

         <div class=course-content>Sorry, no courses are currently scheduled. See <a href="http://dynamictactical.org/courses/" style="color:#000000; text-decoration:underline;">Course Calendar</a> for all upcoming DTT courses.</div> 
         <? endif; 
         wp_reset_query();?> 

回答

1

這是我的小骨骼: 使用布爾標誌來確定您是否需要顯示特殊消息,並僅在循環後顯示消息。 做這樣的事情:

Set $specialmessage = true; 

之前的循環。 然後,在循環內部,當您找到比您測試的日期更近的帖子時,將其設置爲false,並將the_content粘貼到持有者中。您可以使用break退出循環,如果你want.Then,

if($specialmessage) 
    echo 'special message' 
else 
    echo the_content_placeholder. 

不是編碼專家,但我的最好的建議。

0

你扔骨頭:
保持PHP和輸出HTML分開。這樣,您可以在輸出任何內容之前進一步編輯結果,並且稍後可以更容易地進行更改。

相關問題