【问题标题】:Pagination Wordpress with WP_Query使用 WP_Query 分页 Wordpress
【发布时间】:2023-04-01 03:44:01
【问题描述】:

我正在使用 WP 查询来显示我的内容。这是一种自定义帖子类型,称为“enseignement”。查询有效,但如果我想实现分页,那是行不通的。网址在 page/2/ 中进行了转换,但没有显示任何内容。

我的代码

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

你能帮帮我吗?

谢谢!

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    我正在使用这个功能,它可以 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;
        }
      }
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多