【发布时间】:2020-04-22 13:29:48
【问题描述】:
我看到这里已经有很多关于自定义帖子类型分页的问题,但似乎没有一个答案对我有帮助。
我已经创建了一个“推荐”自定义帖子类型,现在我正在尝试使用我已经成功用于标准 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/
我发现“上一个”链接工作正常。与第 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
) );
然后它可以正常工作并显示“« 上一个 1 2 3 4 下一个 »” 很好并且一切正常。但是,我想使用 get_previous_posts_link() 和 get_next_posts_link() 来控制显示为“上一个”和“下一个”链接的文本以及它们周围的环绕元素。
编辑: 根据要求,这是我正在使用的帖子类型注册码:
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');
【问题讨论】:
-
您也可以添加您的
register_post_type参数吗?我认为问题可能来自那里的设置,但我无法在没有看到它们的情况下确认。 -
已添加为上述帖子末尾的编辑。
标签: php wordpress pagination