首页 文章

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

提问于
浏览
0

我有一个在搜索页面上适用于分页的功能:

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设置中的首页,则分页也会中断 .

[编辑](查询代码,如下所述)

$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 : ?>
                
<?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>

1 回答

  • 2

    我没有在您的代码中看到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分页的任何问题的起点 .

相关问题