2010-08-27 101 views
0

在我的WP網站分組活動中,我所說的「事件」在那裏我使用了兩個自定義字段發佈事件信息的類別:WordPress的:每月

  1. EVENTDATE =人類可讀的事件日期
  2. eventsortdate = YYYY/MM/DD以正確的順序列出事件。

我有這段代碼從這裏一個有用的帖子:http://www.davidrisley.com/events-list-with-wordpress/

<?php 
$timecutoff = date("Y-m-d"); 
$args = array(
'category_name' => 'events', 
'orderby' => 'meta_value', 
'meta_key' => 'eventsortdate', 
'meta_compare' => '>=', 
'meta_value' => $timecutoff, 
'order' => 'DESC' 
); 
$my_query = new WP_Query($args); 
if ($my_query->have_posts()) : while ($my_query->have_posts()) : 
$my_query->the_post(); 
$eventdate = get_post_meta($post->ID, "eventdate", true); 
?> 
<ul id="events"> 
<li> 
<strong><?php echo $eventdate; ?></strong><br /> 
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a> 
</li> 
</ul> 
<?php endwhile; else: ?> 
<ul id="events"> 
<li><?php _e('No Events Scheduled! Stay Tuned.'); ?></li> 
</ul> 
<?php endif; ?> 

這將確保事件在正確的順序列出。不過,我還想按月對事件進行分組,因此有一個「月」的標題,並將該月下的所有事件顯示在該標題下。

任何想法非常讚賞,或如何實現這一點的替代建議也非常感謝。謝謝。

編輯:

修訂後的代碼考慮到建議代碼:

<?php 
$timecutoff = date("Y-m-d"); 
$args = array(
'category_name' => 'events-press', 
'orderby' => 'meta_value', 
'meta_key' => 'eventsortdate', 
'meta_compare' => '>=', 
'meta_value' => $timecutoff, 
'order' => 'ASC' 
); 
$my_query = new WP_Query($args); 

if ($my_query->have_posts()) : while ($my_query->have_posts()) : 
$my_query->the_post(); 
$eventdate = get_post_meta($post->ID, "eventdate", true); 
?> 

<?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventdate))){ 
    $currentMonth = date("m", strtotime($eventdate)); 
?> 
<li><?php echo date("m", strtotime($eventdate)); ?></li> 
<?php 
} 
?> 

<ul> 
    <li> 
     <h5><?php echo $eventdate; ?></h5> 
     <h4><?php the_title(); ?></h4> 
     <?php the_content(); ?> 
    </li> 
</ul> 
<?php endwhile; else: ?> 
<ul id="events"> 
<li><?php _e('No Events Scheduled! .'); ?></li> 
</ul> 
<?php endif; ?> 

編輯:進一步修正其功能是否正確:

<?php 
$timecutoff = date("Y-m-d"); 
$args = array(
'category_name' => 'events-press', 
'orderby' => 'meta_value', 
'meta_key' => 'eventsortdate', 
'meta_compare' => '>=', 
'meta_value' => $timecutoff, 
'order' => 'ASC' 
); 
$my_query = new WP_Query($args); 

if ($my_query->have_posts()) : while ($my_query->have_posts()) : 
$my_query->the_post(); 
$eventdate = get_post_meta($post->ID, "eventdate", true); 
$eventsortdate = get_post_meta($post->ID, "eventsortdate", true); 
?> 

<?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventsortdate))){ 
    $currentMonth = date("m", strtotime($eventsortdate)); 
?> 
<li><?php echo date("F", strtotime($eventsortdate)); ?></li> 
<?php 
} 
?> 

<ul> 
    <li> 
     <h5><?php echo $eventdate; ?></h5> 
     <h4><?php the_title(); ?></h4> 
     <?php the_content(); ?> 
    </li> 
</ul> 
<?php endwhile; else: ?> 
<ul id="events"> 
<li><?php _e('No Events Scheduled! .'); ?></li> 
</ul> 
<?php endif; ?> 

回答

1

你可以做令人驚奇的事情與WP_Query()函數,但我認爲gruoping不屬於它。你可以做的是建立一個數組並輸出數組的結果。或者你只是能儘快保存當前的月份和輸出下個月,因爲它的變化:

if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventdate))){ 
    $currentMonth = date("m", strtotime($eventdate)); 
?> 
<li><?php echo date("m", strtotime($eventdate)); ?></li> 
<?php 
} 

這將打印就把每月數第一個事件($ surrentMonth尚未確定,尚),然後再每次有新的一個月。

但是,當然你必須改變你想要它的輸出(LI)。

+0

感謝您的支持 - 我所得到的只是顯示在頁面頂部的「01」。我已經發布了我上面修改的代碼,爲什麼會產生這樣的結果? – Dave 2010-08-31 09:04:50

+0

日期('m')只是一個例子。使用'm'將輸出月份的編號,而'F'將輸出月份的名稱。看到PHP文檔在這裏:http://www.php.net/manual/de/function.date.php – 2ndkauboy 2010-08-31 10:59:27

+0

嘿,非常感謝。隨着一點調整,我已經得到它的工作。非常感謝!! (請參閱上面的調整代碼) – Dave 2010-08-31 12:02:37