按月自定义帖子类型显示内容

时间:2017-06-16 09:27:03

标签: javascript php jquery wordpress

我有一个名为" 活动"的自定义帖子类型。

我想列出一年中可点击的月份列表,然后点击它会显示该月份的所有帖子内容。

因此,每当我点击一个月时,我点击了" 1月和#34; 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; ?>
抱歉,但我不知道这是如何运作的,提前谢谢

2 个答案:

答案 0 :(得分: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>';

答案 1 :(得分: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>;
}