简体   繁体   English

WordPress上一页和下一页不适用于自定义帖子类型

[英]WordPress Prev and Next not working for custom post type

I have a custom post type where I don't want the posts to be visible individually, so I set, 我有一个自定义帖子类型,我不希望该帖子单独显示,因此我设置为

'publicly_queryable' => false,
'rewrite' => array('slug' => 'custom-page-slug'),

I then have the following query on the page: 然后,我在页面上有以下查询:

<?php 
$paged = 2;
$args = array( 'post_type' => 'sample_post_type', 'posts_per_page' => 2, 'paged' => $paged, order => "DESC" );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  echo '<h2>' . esc_html( get_the_title() ) . '</h2>';
  echo '<div class="entry-content">';
  the_content();
  echo '</div>';
endwhile;
?>

My issue is, when I try to use get_next_posts_link() and get_previous_posts_link() , they simply link to the same page (they don't go to page/2 or page/1 ). 我的问题是,当我尝试使用get_next_posts_link()get_previous_posts_link() ,它们只是链接到同一页面(它们不会转到page/2page/1 )。

Here is my code for the prev and next links: 这是我的上一个和下一个链接的代码:

        <?php if ($loop->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
            <div class="clearfix prev-next-arrows">
                <nav class="prev-next-posts">
                    <div class="next-posts-link">
                        <?php echo get_next_posts_link( 'Next Page &raquo;', $query->max_num_pages ); // display older posts link ?>
                    </div>
                    <div class="prev-posts-link">
                        <?php echo get_previous_posts_link( '&laquo; Previous Page' ); // display newer posts link ?>
                    </div>
                </nav>
            </div>
        <?php } ?>

Anyone know what would be wrong with my current setup? 有人知道我当前的设置有什么问题吗?

Thanks 谢谢

I think instead of echo get_next_posts_link(...) and echo get_previous_posts_link(...) you should use next_posts_link(...) and previous_posts_link(...) (without the echo and without the get_ part, at least that's how it works in my websites. 我认为代替echo get_next_posts_link(...)echo get_previous_posts_link(...)您应该使用next_posts_link(...)previous_posts_link(...) (没有echo且没有get_部分,至少这是这样的)在我的网站上工作。

Also, before you define the arguments, insert this line (instead of $paged = 2; ): 另外,在定义参数之前,请插入以下行(而不是$paged = 2; ):

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

I figured it out. 我想到了。 I had the following in my custom post type args: 我的自定义帖子类型args中包含以下内容:

'rewrite' => array('slug' => 'post-type-slug')

It was the same slug as the page I had the loop on 和我循环的页面是一样的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM