【问题标题】:Wordpress: Pagination not working with custom post typeWordpress:分页不适用于自定义帖子类型
【发布时间】: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">&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');

【问题讨论】:

  • 您也可以添加您的register_post_type 参数吗?我认为问题可能来自那里的设置,但我无法在没有看到它们的情况下确认。
  • 已添加为上述帖子末尾的编辑。

标签: php wordpress pagination


【解决方案1】:

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

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

【讨论】:

  • 感谢 jhned - 这是在页面上使用,而不是在帖子上。但无论如何,我没有考虑在我的paginate_links() 参数中使用'type' =&gt; 'array'。这似乎运作良好。谢谢。
  • 关于 WordPress 的部分烦人之处在于术语以及“帖子”如何在很多情况下被重用。页面是具有页面帖子类型的帖子。帖子是具有帖子帖子类型的帖子。附件是具有附件帖子类型的帖子。我所说的“单一帖子”是指“任何帖子类型的任何个人帖子,而不是存档页面”。很抱歉给您带来了困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 2013-07-13
  • 2014-05-15
  • 2011-03-20
相关资源
最近更新 更多