2017-06-16 78 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>; 
} 
相关问题