2016-08-09 3 views
0

サイドバーにカレンダー付きのイベントページがあります。ユーザーが日付をクリックするたびに、作成したAPIエンドポイントを介して、その日付からすべてのイベントがロードされます。 これまでのところすべてが良いですが、ページネーションに問題があります。 ユーザーがイベントページに入力したばかりのページングは​​完璧に機能しますが、ユーザーがAPI経由でイベントを読み込むためにカレンダーに当たってページネーションが破棄され、常にnext_post_linkがページ番号2として返されます。 posts_per_pageはカスタムクエリと同じですが、それは役に立ちません。WordPress APIページネーションは2ページしか返しません。

<?php 

function get_events(WP_REST_Request $request) 
{ 
    global $post; 
    $event_date = $request["event_date"]; 
    $paged = ($request["paged"]) ? $request["paged"] : 1; 
    $date = ''; 

    $args = array(
     'post_type'  => 'event', 
     'posts_per_page' => 3, 
     'paged'   => $paged, 
     'meta_key'  => 'event_date', 
     'meta_value'  => $event_date, 
     'meta_compare' => '>=', 
     'orderby'  => 'meta_value', 
     'order'   => 'ASC' 
    ); 
    $the_query = new WP_Query($args); 

    ob_start(); 

    include(locate_template('events_template.php')); 

    $events = ob_get_contents(); 
    ob_get_clean(); 
    return $events; 
} 

イベントテンプレート:

<div class="posts-container"> 
    <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <div> 
      <?php 
       //Here we group events by event_date and add the date at the top. 
       $event_date = get_post_meta($post->ID, 'event_date', true); 
       if ($event_date != $date) 
       { 
        $event_date_formatted = date('l, F jS, Y', strtotime($event_date)); 
        echo "<p class='page-header'><strong>$event_date_formatted</strong></p>"; 
        $date = $event_date; 
       } 


      ?> 
      <div class="event ath-card"> 
       <h4><?php the_title(); ?></h4> 
       <p><?php echo get_post_meta($post->ID, 'event_location', true) ?></p> 
       <p><?php echo get_post_meta($post->ID, 'event_details', true) ?></p> 
      </div> 
     </div> 
    <?php endwhile; endif; ?> 
</div> 

<div class="loader text-center hidden"> 
    <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i> 
</div> 

<div class="div pagination-container text-center"> 
    <?php 
     //always returns page number 2. 
     //I run the exact query in the page itself and it works fine 
     next_posts_link('<button class="ath-btn ath-btn-info">LOAD MORE</button>', $the_query->max_num_pages); 
    ?> 
</div> 

答えて

0

まあ、私は通常の方法で問題を解決することができませんでしたが、幸運にも

<?php $the_query->max_num_pages ?> 

はので、私はそれを使用し、正しい総ページ数を返さ次の投稿リンクを手動で設定します。

if($paged < $the_query->max_num_pages) 
{ 
    $next_page = $paged + 1; 
} 
else 
{ 
    $next_page = null; 
} 

そして、この:

<?php if ($next_page) : ?> 
    <a href="http://localhost:8888/water/wp-json/events/v1/events/page/<?php echo $next_page ?>/?event_date=<?php echo $request["event_date"] ?> "> 
     <button class='ath-btn ath-btn-info'>LOAD MORE</button> 
    </a> 
<?php endif; ?> 

は、私はこれを追加しました
関連する問題