我看到这里已经有很多关于自定义帖子类型分页的问题了,但似乎没有一个答案对我有帮助。
我已经创建了一个"Testimonials"自定义帖子类型,现在我正试图使用我在标准Wordpress帖子中成功使用的代码为其添加分页,但它与此不太配合。
现在,它都是作为一个短代码构建的,所以所有的东西都必须放入$output中,然后返回。
这是我的:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'orderby' => $a['orderby'],
'order' => $a['order'],
'posts_per_page' => $testimonials_per_page,
'paged' => $paged
);
$testimonials = new WP_Query( $args );
if( $testimonials->have_posts() ) :
while( $testimonials->have_posts() ) :
$output = '...'
// BLAH BLAH BLAH, YADA YADA YADA...
endwhile;
$next_posts_link = '<span class="testimonial_pagination_next">' . get_next_posts_link($next_text) . '</span>';
if(get_previous_posts_link()){
$next_posts_link = '<span class="testimonial_prev_next_separator"> | </span><span class="testimonial_pagination_next">' . get_next_posts_link($next_text) . '</span>';
$prev_posts_link = '<span class="testimonial_pagination_prev">' . get_previous_posts_link($prev_text) . '</span>';
} else {
$prev_posts_link = "";
}
$output .= '<div class="testimonial_pagination_links">';
$output .= $prev_posts_link;
$output .= $next_posts_link;
$output .= '</div>';
wp_reset_postdata();
else :
$testimonialsOutput .= 'No testimonials to display';
endif;
return $output;
乍一看,它似乎根本不起作用。http://sandbox.graphicdetail.co.nz/testimonials-pagination-test/
但是如果我转到第2页,http://sandbox.graphicdetail.co.nz/testimonials-pagination-test/page/2/我发现"Previ"链接运行良好。与第3页、第4页等相同。因此,看起来get_previous_posts_link()
工作正常,但get_next_posts_link()
工作不正常。
这对我来说似乎很奇怪,一个应该起作用,但另一个不起作用。
如果我使用这个代码:
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $testimonials->max_num_pages
) );
然后它工作得很好,并显示,"«上一个12 3 4下一个»"很好,一切都工作。但是,我想使用get_previous_posts_link()
和get_next_posts_link()
,这样我就可以控制显示为"上一个"one_answers"下一个"链接的文本,以及它们周围的换行元素。
编辑:根据请求,这是我正在使用的post-type注册代码:
function testimonials_posttype_register() {
$labels = array(
'name' => _x('Testimonials', 'post type general name'),
'singular_name' => _x('Testimonial', 'post type singular name'),
'add_new' => _x('Add New', 'portfolio item'),
'add_new_item' => __('Add New Testimonial'),
'edit_item' => __('Edit Testimonial'),
'new_item' => __('New Testimonial'),
'view_item' => __('View Testimonial'),
'search_items' => __('Search Testimonial'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => plugins_url('img/testimonials-icon.png',__FILE__ ),
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','excerpt','editor','thumbnail','revisions')
);
register_post_type( 'testimonial' , $args );
}
add_action('init', 'testimonials_posttype_register');
我可以看到部分问题:如果is_single()
为true,则get_previous_posts_link不会返回任何内容。因此,如果你在单个帖子上使用这个短代码,链接就不会出现。get_next_posts_link
也是如此。
我建议使用分页链接的next_text
和prev_text
参数来解决这个问题。您可以将type
参数设置为array
,然后获取数组中的第一个和最后一个项,以避免使用"1 2 3 4"链接。