在我的wiget自定义帖子类型列表中获取分页

时间:2013-08-12 13:28:15

标签: wordpress pagination custom-post-type

我在小部件中按类别获取自定义帖子类型。现在我想对这些帖子进行分页,如果那些超过10个帖子。对于我尝试使用此代码

<ul class="posts-list">
    <?php
            $cats = get_the_category();
            $cat_name = $cats[0]->name;
            $paged= isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
        query_posts(array ( 'category_name' => $cat_name, 'posts_per_page' => 6, 'paged' => $paged));
        while (have_posts()) : the_post();
            // do whatever you want 
    ?>
    <li>
        <h5><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h5>
        <p><strong>posted on</strong> <?php the_time(' F jS, Y') ?> <strong>by</strong> <?php the_author(); ?></p>
        <p><strong>Posted in</strong> <span><?php print_r($cat_name); ?></span></p> 
        <?php
        endwhile;
        ?>
    </li>
<?php
$pag_args1 = array(
    'format'   => '?paged=%#%',
    'current'  => $paged,
    'total'    => 3,
    'add_args' => array( 'paged' => $paged)
);
echo paginate_links( $pag_args1 ); ?>
</ul>

但我无法得到确切的结果。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下

<ul class="posts-list">
    <?php
            $cats = get_the_category();
            $cat_name = $cats[0]->name;
            if ( get_query_var('paged') )
          { $paged = get_query_var('paged'); }
          elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
         else { $paged = 1; } 
        query_posts(array ( 'category_name' => $cat_name, 'posts_per_page' => 6, 'paged' => $paged));
        while (have_posts()) : the_post();
            // do whatever you want 
    ?>
    <li>
        <h5><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h5>
        <p><strong>posted on</strong> <?php the_time(' F jS, Y') ?> <strong>by</strong> <?php the_author(); ?></p>
        <p><strong>Posted in</strong> <span><?php print_r($cat_name); ?></span></p> 
        <?php
        endwhile;
        ?>
    </li>
<?php
$pag_args1 = array(
    'format'   => '?paged=%#%',
    'current'  => $paged,
    'total'    => 3,
    'add_args' => array( 'paged' => $paged)
);
echo paginate_links( $pag_args1 ); ?>
</ul>
相关问题