2017-03-09 99 views
0

我使用WP查询来显示我的内容。这是一个自定义的帖子类型调用“enseignement”。查询工作,但如果我想实现分页,这是行不通的。该URL在第2/2页中被转换,但没有任何显示。WP_Query的分页Wordpress

我的代码

  <?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 


       $args = array(
       'post_type' => 'enseignement', 
       'posts_per_page' => 2, 
       'paged'   => $paged, 
       'meta_query' => array(
         'relation' => 'AND', 
         array(
          'key' => 'cycle', // name of custom field 
          'value' => $_GET['cycle'], // matches exactly "red" 
          'compare' => 'LIKE', 
                 ), 
       array(
        'key'  => 'lieu', 
        'value' => $_GET['lieu'], 
        'compare' => 'LIKE', 

     ), 
    ), 


       ); 
      $loop = new WP_Query($args); ?> 
      <?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?> 
      <?php get_template_part('content', 'enseignement', get_post_format());?> 

     <?php endwhile; ?> 

<?php 
next_posts_link('Older Entries', $loop->max_num_pages); 
      previous_posts_link('Next Entries &raquo;'); 
      wp_reset_query(); 
?> 
       <?php endif; ?> 

你能帮助我吗?

谢谢!

回答

0

我正在使用此功能,它工作100%。适应自己,一切都会好的。

<?php 

function generatePagination($paged, $loop, $echo = TRUE) { 

    $big = 999999999; // need an unlikely integer 

    $pages = paginate_links(array(
    'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 
    'format' => '?paged=%#%', 
    'current' => max(1, $paged), 
    'total' => $loop->max_num_pages, 
    'type' => 'array', 
    'total' => $loop->max_num_pages, 
    'prev_next' => TRUE, 
    'prev_text' => '<span aria-hidden="true">&laquo;</span>', 
    'next_text' => '<span aria-hidden="true">&raquo;</span>'  
)); 

    if (is_array($pages)) { 

    $prevText = '<li class="paginated_link disabled"><span aria-hidden="true">&laquo;</span></li>'; 
    $nextText = '<li class="paginated_link disabled"><span aria-hidden="true">&raquo;</span></li>'; 

    $pagination = '<ul class="pagination">'; 
    if ($paged == 0 || $paged == 1) { 
     $pagination .= $prevText; 
    } 
    foreach ($pages as $page) { 
     $pagination .= '<li class="paginated_link'; 
     if (strpos($page, 'current') !== false) { 
     $pagination .= ' active'; 
     } 
     $pagination .= '">' . $page . '</li>'; 
    } 
    if ($loop->max_num_pages == $paged) { 
     $pagination .= $nextText; 
    } 
    $pagination .= '</ul>'; 

    if ($echo) { 
     echo $pagination; 
    } else { 
     return $pagination; 
    } 
    } 
} 

?>