Wordpress:分页不适用于自定义帖子类型

时间:2020-04-22 13:29:48

标签: php wordpress pagination

我看到这里已经有很多关于自定义帖子类型分页的问题,但是似乎没有一个答案对我有帮助。

我创建了一个“推荐”自定义帖子类型,现在我尝试使用我在标准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">&nbsp;&nbsp;|&nbsp;&nbsp;</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');

1 个答案:

答案 0 :(得分:1)

我可以看到部分问题:如果is_single()为true,get_previous_posts_link不会返回任何内容。因此,如果您在单个帖子上使用此短代码,则该链接将不会显示。 get_next_posts_link也是如此。

我建议使用paginate linksnext_textprev_text参数来解决这个问题。您可以将type arg设置为array,然后获取数组中的第一项和最后一项,以避免也使用“ 1 2 3 4”链接。

相关问题