NEXT_POSTS_LINK URL不包含自定义帖子类型的名称

时间:2021-02-01 作者:slammedacura27

在我的索引中。php中,我有一个新的WP\\u Query(),用于自定义帖子类型,我将其命名为;待办事项;。

我正在尝试为此查询实现分页,因此它只显示三篇文章,然后需要转到下一页查看更多内容。

但是,对于while循环之后的代码:

<li><?php next_posts_link( \'NEXT\', $todos->max_num_pages) ?></li>
实际链接本身将我带到localhost/page/2.

当它应该把我带到localhost/todo/page/2, 当我手动输入这个URL时,它会显示接下来的三篇文章,而不是另一个URL,它会将我带到404。

我对Wordpress很陌生,可能会错过这个问题的一些关键细节,以及一些事情如何运作的基本知识,但如果有人知道我做错了什么,我将非常感谢任何帮助。。

以下是整个代码:

if ( get_query_var( \'paged\' ) ) { $paged = get_query_var( \'paged\' ); }
elseif ( get_query_var( \'page\' ) ) { $paged = get_query_var( \'page\' ); }
else { $paged = 1; }

$args = array(
  \'posts_per_page\' => 3,
  \'post_type\'   => \'todo\',
  \'post_status\' => \'publish\',
  \'page\' => $paged
 );
 
$todos = new WP_Query( $args );
?>

    <main id="primary" class="site-main">
        <section class="glass">
            <div class="todolist">
            <?php
            if( $todos->have_posts() ) :
                while($todos->have_posts()) :
                $todos->the_post();
            ?>
                <div class="todo">
                    <img src="<?php echo get_template_directory_uri() . \'/event_note-24px\'?>" alt="">
                    <div class="details">
                        <h2><?php echo get_the_title();?></h2>
                        <div class="time">
                            <img src="<?php echo get_template_directory_uri() . \'/query_builder-24px\'?>" alt="">
                            <?php echo get_the_content();?>
                        </div>
                    </div>
                    <div class="todoaction">
                        <img src="<?php echo get_template_directory_uri() . \'/done-24px\'?>" alt="">
                        <img src="<?php echo get_template_directory_uri() . \'/clear-24px\'?>" alt="">
                    </div>
                </div>
                <?php endwhile;?>
            </div>
            <li><?php previous_posts_link( \' PREV\', $todos->max_num_pages) ?></li> 
            <li><?php next_posts_link( \'NEXT\', $todos->max_num_pages) ?></li>
            <?php endif; ?>
        </section>
谢谢你。

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

That is actually the expected output of next_posts_link(), i.e. it always uses the current page URL. And the same also applies to previous_posts_link().

So if you\'re on the homepage (localhost/), then the URL of page 2 would be localhost/page/2, and if you\'re on (your custom post type archive page at) localhost/todo, then the URL of page 2 would be localhost/todo/page/2.

And if you want to alter the main WordPress query on the homepage, then you should use the pre_get_posts hook instead of making a secondary/custom WP_Query call (new WP_Query()) in your index.php template.

So for example, to display 3 todo posts on the homepage,

  1. Add this to your theme\'s functions.php file:

    add_action( \'pre_get_posts\', \'my_pre_get_posts\' );
    function my_pre_get_posts( $query ) {
        // If we\'re on the homepage, query 3 "todo" posts instead of the default ones.
        if ( is_home() ) {
            $query->set( \'post_type\', \'todo\' ); // query posts of the "todo" type only
            $query->set( \'posts_per_page\', 3 ); // and display three posts per page
        }
    }
    
  2. Then in your index.php template, just remove those $todos and $args, and use the default loop to display the posts returned via the main query:

    <?php
    // No need for the $todos and $args.
    ?>
    
        <main id="primary" class="site-main">
            <section class="glass">
                <?php if ( have_posts() ) : ?>
                    <div class="todolist">
                        <?php while ( have_posts() ) : the_post(); ?>
                            <div class="todo">
                                ... your code.
                            </div>
                        <?php endwhile; ?>
                    </div>
    
                    ... your pagination here.
                <?php endif; // end have_posts() ?>
            </section>
    

That way, even if next_posts_link() and previous_posts_link() do not add the todo/ to the pagination links, you wouldn\'t get the error 404 when going to localhost/page/2 unless of course if there\'s no page 2 for the main query.

And if you wonder what that "main" (WordPress) query is, then check the Codex, specifically the part which says, "Save the posts in the $wp_query object to be used in the WordPress Loop.".

Also, your pagination code will show an empty bullet when there\'s no next/previous posts link, so to fix that, you\'d want to use get_previous_posts_link() instead of previous_posts_link() and get_next_posts_link() instead of next_posts_link(). Example:

<?php // this would replace the "... your pagination here." part above.

$links = \'\';

if ( $link = get_previous_posts_link( \'Previous Posts\' ) ) {
    $links .= "<li>$link</li>";
}

if ( $link = get_next_posts_link( \'Next Posts\' ) ) {
    $links .= "<li>$link</li>";
}

if ( $links ) {
    echo "<ul>$links</ul>";
}
?>

Additionally, in your $args array, you should\'ve used paged (note the "d") and not page, i.e. \'paged\' => $paged. But I\'m just saying that so you know the correct arg is paged. :)

相关推荐

pagination in WP rest api

我们已经制作了一个自定义API来按降序获取所有帖子,我们希望在该API中添加分页。我已经阅读了其他问题和答案,但没有领会其中的意思。谁能用一个简单的分页代码向我解释一下,这样我就能理解它是如何工作的?以下是我迄今为止所做的代码:define(\'API_ENDPOINT_VERSION\',1); //flush the rewrite rules on plugin activation function apiendpoint_activate() {&#x