小部件中我的自定义帖子类型的分页 [英] Pagination for my custom post type in widget

查看:79
本文介绍了小部件中我的自定义帖子类型的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在窗口小部件中显示了自定义帖子类型.现在,我想在最后添加分页.因为我的自定义帖子类型中有10多个帖子.

I have displayed the custom post type in the widget. Now i want to add pagination in the last. because i have more than 10 posts in my custom post type.

<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php

global $post;
    $cats = get_the_category();
    $cat_name = $cats[0]->name;
    $args = array(
    'posts_per_page'   => 10,
    'offset'           => 0,
    'category'         => $cat_name,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_status'      => 'publish',
    'suppress_filters' => true );

$previous_post = get_posts($args);
foreach ( $previous_post as $post ) : 
  setup_postdata( $post ); ?>
    <li>
        <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
        <p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
        <p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>    
    </li>
<?php endforeach;
wp_reset_postdata(); ?>
<?php endif; ?>
</ul>

推荐答案

尝试此方法,然后在'post_type' => 'your custom post type name'

<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php

global $post;
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
    $cats = get_the_category();
    $cat_name = $cats[0]->name;
    $args = array(
    'posts_per_page'   => 10,
    'offset'           => 0,
    'category'         => $cat_name,
    'orderby'          => 'post_date',
    'paged'          => $paged1,
    'post_type' => 'your custom post type name'
    'order'            => 'DESC',
    'post_status'      => 'publish',
    'suppress_filters' => true );

$previous_post = get_posts($args);
foreach ( $previous_post as $post ) : 
  setup_postdata( $post ); ?>
    <li>
        <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
        <p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
        <p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>    
    </li>
<?php endforeach;
?>
<?php endif; 

$pag_args1 = array(
    'format'   => '?paged1=%#%',
    'current'  => $paged1,
    'total'    => $previous_post->max_num_pages,
    'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args1 );
 wp_reset_postdata();  ?>

</ul>

这篇关于小部件中我的自定义帖子类型的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆