doumo1807831 2014-05-23 09:37
浏览 95
已采纳

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

I have a function that works fine for pagination on a search page:

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>
";
     }
}

And I call it using this code after the wp_query:

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

The wp_query has paged' => $paged, in the arguments.

But, when I try and paginate the homepage the pagination doesn't actually work. It'll link to the correct URL, but the posts don't actually change.

The home page has been made a page template (not using index.php) and in settings the front page has been set to the correct page.

But I have no idea why it's not paginating on the homepage but is on the search page. If I set the search page to the front page in WP Settings that pagination also breaks.

[EDIT] (code for query, as requested below)

$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>
  • 写回答

1条回答 默认 最新

  • dongpei3245 2014-05-27 09:23
    关注

    I don't see in your code where you check if the guery_var contains a paged variable, you should add this code to check if it exists and if the value is added to it.

    this is the code:

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

    This is also a starting point for debugging any issues with wordpress pagination.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Keil编译时报错"no source": Error: #5: cannot open source
  • ¥50 操作系统时间无法更新
  • ¥20 Windows11, usb转hdmi,外接显示器无反应
  • ¥20 怎么在JavaFx的TableView中动态地添加数据。
  • ¥15 MFC里的工具栏按钮图标使用外部图片
  • ¥15 如何在 llama.cpp 服务器中实现用户登录功能的优化方案?(语言-c++)
  • ¥15 有会用octave 的吗,急!代做!好偿!
  • ¥15 有一套同城小程序源码,Uniapp前端,php+html+mysql后端 ,能不能教我搭建起来可以运行,我不知道怎样操作
  • ¥15 mac调用java.io接口无法在根目录生成文件
  • ¥15 java微服务节点假死,网关路由时长延迟