当前分页链接未激活,Next,Previous不起作用

时间:2016-09-08 09:54:45

标签: php mysql wordpress pagination

我正在尝试通过自定义查询从数据库中检索所有帖子时实施 PAGINATION或PAGING 。我想在每个页面上显示3个帖子。逻辑明智,它运作良好。防爆。如果我点击第一页它显示1,2,3帖子,如果我点击第二页它显示4,5,6帖子。

但问题是 - 1.如果我点击1或2或3之类的任何页码链接,那么每次选择的页面都应该是活动的,但在我的程序中,总是第一页码链接处于活动状态。

  1. 如果我单击“下一步”,它将始终转到第二页。它应该转到当前页面的下一页。

  2. 以前的链接无法显示。

    <?php 
    $per_page = 3;
    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 0;
    if($page>0)
    $page = $page*$per_page-$per_page;
    
    $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->postmeta.meta_key = '_edit_last' 
    AND $wpdb->postmeta.meta_value = '1' 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC LIMIT $per_page offset $page";
    
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    ?>
    <?php if ($pageposts): ?>
    <?php global $post;?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post);?>
    
    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent     Link to <?php the_title_attribute(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <div class="entry">
       <?php the_content('Read the rest of this entry »'); ?>
    </div>
    <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    

       
  3. 分页

     <?php 
    $querystr2 = "
        SELECT count(*)
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
        AND $wpdb->postmeta.meta_key = '_edit_last' 
        AND $wpdb->postmeta.meta_value = '1' 
        AND $wpdb->posts.post_status = 'publish' 
        AND $wpdb->posts.post_type = 'post'
        AND $wpdb->posts.post_date < NOW()
        ORDER BY $wpdb->posts.post_date DESC
     ";
    $totalPosts = $wpdb->get_var($querystr2);
    
    echo paginate_links( array(
     'base' => add_query_arg( 'cpage', '%#%' ),
        'format' => '',
        'prev_text' => __('Previous'),
        'next_text' => __('Next'),
        'total' => ceil($totalPosts / $per_page),
        'current' => $page
    ));
    
    ?>
    

    任何人都可以尝试解决此问题或在我的代码中发现错误吗?

1 个答案:

答案 0 :(得分:0)

最后,此链接解决了有关在自定义页面或模板文件上显示WP帖子的所有问题。

Custom WordPress loop with pagination