Wordpress类别帖子页面模板中的分页

时间:2014-03-17 19:19:31

标签: wordpress list posts

我使用下面的代码在自定义页面中显示包含特定类别帖子的列表。我想在底部添加分页,但直到现在,我都失败了。

以某种方式添加它的任何方式?

           <?php $cat_id = 22;
             $cat_link = get_category_link( $cat_id );
             $cat_name = get_cat_name($cat_id); ?>


       <div class="catrecent">
           <?php    $query = new wp_query();
           $query->query('showposts=1&cat=' .  $cat_id);
           if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
               <div class="recenttitle">
                   <h2 class="catidtxt"> <a href="<?php echo ($cat_link); ?>" title="<?php echo ($cat_name); ?>"><?php echo ($cat_name); ?></a></h2>
                   <h2 class="recentposttitle"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>    
               </div>

               <div class="recentpostwrap">
                    <?php the_post_thumbnail( 'catlarge' ); ?>
                    <?php the_content_limit(600,""); ?>
               </div>


              <?php endwhile; else: ?><?php endif; ?><?php wp_reset_query(); ?>
       </div>

       <ul class="posts_list"> 
           <?php    $query = new wp_query();
           $query->query('offset=1&showposts=4&cat=22&paged=' .  $cat_id);
           if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>

                                <li>
                                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'catmed' ); ?></a>
                                    <a href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?> </a>
                                </li>

           <?php endwhile; else: ?><?php endif; ?><?php wp_reset_query(); ?>
       </ul>

1 个答案:

答案 0 :(得分:1)

有用的链接:http://codex.wordpress.org/Function_Reference/paginate_linkshttp://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

尝试这样的事情

<ul class="posts_list"> 
    <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $ppp = 4;
    $offset = 1;

    //Manually determine page query offset (offset + current page (minus one) x posts per page)
    $page_offset = $offset + ( ($paged - 1) * $ppp );

    $query = new wp_query(array(
        'offset'         => $page_offset,
        'posts_per_page' => $ppp,
        'cat'            => $cat_id,
        'paged'          => $paged
    ));

    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>

    <li>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'catmed' ); ?></a>
        <a href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?> </a>
    </li>

    <?php endwhile; else: ?><?php endif; ?>
</ul>

<?php
// pagination
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
    'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) )    ),
    'total'   => ceil(($query->found_posts - $offset) / $ppp),
    'format'  => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
));

wp_reset_query();

&GT;