Index.php上的分页在第一个页面上不起作用

时间:2015-12-20 作者:Sergei Rodionov

我有index.php 使用分页。永久链接设置为普通永久链接。

打开页面时有正确的分页视图/?paged=2 但如果我打开index.php 我所有的帖子都没有分页。这是我的index.php:

<?php get_header(); ?>

<?php

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

$posts_per_page = get_option(\'posts_per_page\');

$args = array(\'post__in\' => get_option( \'sticky_posts\' 
),\'posts_per_page\' => $posts_per_page, \'paged\' => $paged);

$the_query = new WP_Query( $args);


if ( $the_query->have_posts() ) : ?>


<div class="section">
<div class="container">
    <div class="wrap-content">
        <div class="pg-block list">
        <?php
            while ( $the_query->have_posts() ) : $the_query->the_post();
                get_template_part( \'content\', get_post_format() );
            endwhile;
        ?>
        </div>

        <?php wp_reset_postdata(); ?>

        <aside class="wrap-aside hid-xs">
            <div class="block-inner">
            <?php dynamic_sidebar(\'sidebar-1\');?>
            </div>
        </aside>
    </div>
    <div class="wrap-content">
        <?php
        //number of pages for pagination
        $total_pages =  $the_query->found_posts / $posts_per_page;

        if ($the_query->found_posts % $posts_per_page != 0) $total_pages++;
            $args_nav = array(
            \'show_all\'     => false, 
            \'end_size\'     => 2,     
            \'mid_size\'     => 2,     
            \'prev_next\'    => True,  
            \'prev_text\'    => \'\',    
            \'next_text\'    => \'\',    
            \'total\'        => $total_pages, 
            \'add_args\'     => False,
            \'add_fragment\' => \'\',     
            \'screen_reader_text\' =>  \' \'
        );
            the_posts_pagination($args_nav);
        ?>
    </div>
</div>
</div>


<?php else : ?>
<p><?php _e( \'Posts not found\' ); ?></p>
<?php endif;

get_footer();?>
附言:我只是像那样编辑我的代码,但什么都没有改变。第一页显示所有未分页的帖子

<?php if ( have_posts() ) : ?>

<div class="section">
<div class="container">
    <div class="wrap-content">
        <div class="pg-block list">
        <?php
            while ( have_posts() ) : the_post();
                get_template_part( \'content\', get_post_format() );
            endwhile;
        ?>
        </div>

        <?php wp_reset_postdata(); ?>

2 个回复
SO网友:Varsha Dhadge

在数组中添加忽略粘滞线可以解决以下问题:)

$args = array(
       \'post_type\'      => \'post\',
       \'posts_per_page\' => \'6\',
       \'ignore_sticky_posts\' => 1,//this is the one :)
       \'paged\' => $paged,           
);

SO网友:franz

将这两行添加到主题的函数中。php文件和所有内容都将恢复工作:

add_filter(‘redirect_canonical’,’custom_disable_redirect_canonical’);

function custom_disable_redirect_canonical($redirect_url) {if (is_paged() && is_singular()) $redirect_url = false; return $redirect_url; }

相关推荐

如何为class-wp-query.php追踪这些PHP通知和警告的原因?

我的WordPress调试。日志文件中充满了这些PHP通知,间隔大约15分钟,然后是几个小时…[23-Mar-2018 05:33:00 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3736 [23-Mar-2018 05:33:00 UTC] PHP Notice: Trying to get p