Wordpress: 自定义文章类型无法使用分页

问题描述 投票:0回答:1

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

我已经创建了一个 "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">&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.nztestimonials-pagination-test。

但是,如果我到第二页。http:/sandbox.graphicdetail.co.nztestimonials-pagination-testpage2。我发现 "上一页 "的链接很好用。 第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() 这样我就可以控制 "上一页 "和 "下一页 "链接的文字显示,以及它们周围的包装元素。

EDIT:根据要求,这里是我正在使用的帖子类型注册代码。

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');
php wordpress pagination
1个回答
1
投票

我可以看到部分问题。get_previous_posts_link 不返回任何东西,如果 is_single() 是真的。所以如果你在单篇文章上使用这个短码,链接就不会显示出来。同样的事情也适用于 get_next_posts_link.

我建议使用 next_textprev_text 琶音 分页链接 为了解决这个问题。您可以设置 type 辩称 array然后抓取数组中的第一个和最后一个项目,以避免也使用 "1 2 3 4 "链接。

© www.soinside.com 2019 - 2024. All rights reserved.