2017-06-16 18 views
0

私はカスタム投稿タイプが「イベント」です。カスタム投稿タイプのコンテンツを月別に表示

1ヶ月から1ヶ月のリストを作成したいと思っていました。クリックするとその月のすべての投稿コンテンツが表示されます。私は、例えば、月をクリックするたび

は、だから私は、それはそれの下で、その月から、すべての記事が表示されます2017年

の「1月」をクリック。私は

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 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); 

私function.phpファイルで使用すると、毎月の

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

して、リストを表示したい場所に、今あなたは、ワードプレス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>; 
} 
関連する問題