2017-06-16 59 views
0

我有一個自定義帖子類型,名爲「事件」。按月自定義帖子類型顯示內容

我想列出一年中可點擊的月份,然後點擊它時會顯示該月份的所有帖子內容。

所以每當我在一個月點擊比如我點擊了2017年

「一月」它會顯示該月份的所有帖子下它。下面的例子

enter image description here

我這裏列出了幾個月,但鏈接的代碼不工作

<?php 
global $wpdb; 
$limit = 0; 
$year_prev = null; 
$months = $wpdb->get_results("SELECT DISTINCT MONTH(post_date) AS month , YEAR(post_date) AS year, COUNT(id) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now() and post_type = 'events' GROUP BY month , year ORDER BY post_date ASC"); 
foreach($months as $month) : 
    $year_current = $month->year; 
    if ($year_current != $year_prev){ 
     if ($year_prev != null){?> 

     <?php } ?> 

    <div class="archive-year"><strong><?php echo $month->year; ?></strong></div> 

    <?php } ?> 
    <div><a href="#"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></div> 
<?php $year_prev = $year_current; 

if(++$limit >= 18) { break; } 

endforeach; ?> 

抱歉,但我不知道這是如何工作的,在此先感謝

+0

你面對什麼樣的錯誤?無論如何,據我所知,你想列出所有月份的所有帖子,對吧?但在你的查詢中,我發現'和post_date <= now()',正如你所看到的,'now()'不是一個月... – vietnguyen09

+0

沒有錯誤,我只想點擊月份在那個月下的所有帖子都會顯示在下面,對不起,我剛剛複製了那個代碼來列出幾個月,我對PHP不太瞭解 –

回答

0

我已經做了這個功能。在我function.php文件

function post_type_Custom($where,$args){ 
    $post_type = isset($args['post_type']) ? $args['post_type'] : 'post'; 
    $where = "WHERE post_type = '$post_type' AND post_status = 'publish'"; 
    return $where; 
} 
add_filter('getarchives_where','post_type_Custom',10,2); 

現在,無論你想通過每月

$args = array(
    'post_type' => 'custom_post_type', 
    'type'   => 'monthly', 
    'echo'   => 0 
); 
echo '<ul>'.wp_get_archives($args).'</ul>'; 
0

顯示列表試試這個

你可以使用WordPress date_query

做添加以下代碼
$args = array(
    'post_type' =>'events', 
    'post_status' => 'publish', 
    'date_query' => array(
     array(
      'year' => 2017, 
      'month' => 1 
     ), 
    ), 
); 

$query = new WP_Query($args); 
$events = $query->posts; 
foreach ($events as $event) { 
    $event->ID:$event->post_title.<br>; 
} 
相關問題