WordPress分页工作,但不是在主页上


WordPress pagination works, but not on homepage

我有一个可以很好地在搜索页面上进行分页的函数:

function pagination($pages = '', $range = 2)
{  
     $showitems = ($range * 2)+1;  
     global $paged;
     if(empty($paged)) $paged = 1;
     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   
     if(1 != $pages)
     {
         echo "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";
         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+5 || $i <= $paged-$range-5) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }
         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>'n";
     }
}

我用wp_query后面的代码来命名它:

<?php pagination($pages = $wp_query->max_num_pages); ?>

wp_query参数中有paged' => $paged,

但是,当我尝试对主页进行分页时,分页实际上不起作用。它会链接到正确的URL,但帖子实际上并没有改变。

首页已经做了一个页面模板(不使用index.php),并且在设置中首页已经设置为正确的页面。

但是我不知道为什么它不在主页上分页,而是在搜索页面上。如果我在WP设置中将搜索页面设置为首页,那么分页也会中断。

[EDIT](查询代码,如下所示)

$args = array(  
    // general
    'post__in' => $postIDs,
    'post_type' => 'event',
    'posts_per_page' => 10,
    'paged' => $paged,
    'meta_key' => $_SESSION['search']['sort-key'],
    'orderby' => $_SESSION['search']['sort-by'],
    'order' => 'ASC',

    // what input
    'title_like' => $_SESSION['search']['keyword'],
    // change to sub categories/ keywords
    // category filter
    'tax_query' => array(
        array(
            'taxonomy' => 'main-cat',
            'field' => 'slug',
            'terms' => $mainCat
        )
        // add sub category search too
    ),
        // date filter
        'meta_query' => array(
        array(
            'key' => 'date_%_start-date',
            'value' => $when,
            'compare' => '>=',
            'type' => 'NUMERIC'
        ),
        array (
            'key' => 'date_%_end-date',
            'value' => $when2,
            'compare' => '<=',
            'type' => 'NUMERIC'
        )
    )

);

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <div class="event clearfix">
        <div class="event-image grid-1-4 no-padding">
            <?php if( get_field('images') ) : ?>
                <?php $images = get_field('images'); ?>
                <img src="<?php echo $images[0][sizes][thumbnail]; ?>" alt="<?php the_title(); ?>" />
            <?php else : ?>
                <br />
            <?php endif; ?>
        </div>
        <div class="event-info grid-1-2 clearfix">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php include('date.php'); ?>
            <?php 
                $minidescription = get_field('mini-description'); 
                $minidescription = html_entity_decode($minidescription);
                echo '<p>' . $minidescription . '</p>';
            ?>
        </div>
        <div class="event-details grid-1-4 no-padding">
            <div class="detail">Distance <?php echo round($post->distance, 1); ?> mi</div>
            <?php if( get_field('adult') == '' || get_field('adult') == '0' ) : ?>
                <div class="detail">Price: Free</div>
            <?php else : ?>
                <div class="detail">Price: &pound;<?php the_field('adult'); ?></div>
            <?php endif; ?>
            <?php include('countdown.php'); ?>
        </div>

    </div>  
    <?php $plus++; ?>
<?php endwhile; ?>  
<div class="grid-100">
    <?php pagination(); ?>
</div>

我在你的代码中没有看到你检查guery_var是否包含一个分页变量,你应该添加这段代码来检查它是否存在,如果值被添加到它

这是代码:

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

这也是调试wordpress分页问题的起点。