Pagination in WP_Query?

时间:2016-04-07 作者:vlovsky

我正在使用WP\\U查询显示类别内的帖子和选定的自定义术语(“健康可见性”在类别“文章”中)

我的代码:

    <?php $catquery = new WP_Query( \'post_type=article&posts_per_page=4&article-visibility=fitness\' );
while($catquery->have_posts()) : $catquery->the_post();
?>

    <article class="post home-post">
        <a href="<?php the_permalink(); ?>">
            <div class="post-thumbnail-img">
                <?php the_post_thumbnail(\'small-thumbnail\'); ?>
            </div>
            <h2><?php the_title(); ?></h2>
        </a>
        <p class="post-info">
            <?php the_time(\'F jS, Y\'); ?> |     
            <?php get_template_part(\'block-athlete-archive\'); ?>
         </p>

        <p>
            <?php echo get_the_excerpt(); ?>
            <span class="readmore">
                <br><a href="<?php the_permalink(); ?>">Read more &raquo;</a>
            </span>
        </p>

    </article>

<?php endwhile; ?>`
现在,在我的归档页面上,我使用以下代码进行分页:

            <div class="pagination">
                <?php wpex_pagination(); ?>
            </div>
我把它放在“endwhile”之后,它就起作用了。

WP\\U查询并非如此。

如何解决此问题?

1 个回复
SO网友:Owais Alam

将以下代码放入函数中。php

# +++++++++++++++++++++++++++++++Pagination ++++++++++++++++++++++++++++++++++++++
function kriesi_pagination($pages = \'\', $range = 2)
{
    $prev = __(\'« Prev\');
    $next = __(\'Next »\');
    $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 "<ul class=\'pagination\'>";
        if($paged > 2 && $paged > $range+1 && $showitems < $pages && false)
            echo "<li><a href=\'".get_pagenum_link(1)."\'>&laquo;</a></li>";
        if($paged > 1 && $showitems < $pages)
            echo "<li><a href=\'".get_pagenum_link($paged - 1)."\'>$prev</a></li>";

        for ($i=1; $i <= $pages; $i++) {
            if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
                echo ($paged == $i)? "<li class=\'active\'><a href=\'javascript:void(0)\'>".$i."</a></li>":"<li class=\'inactive\'><a href=\'".get_pagenum_link($i)."\'>".$i."</a></li>";
            }
        }

        if ($paged < $pages && $showitems < $pages)
            echo "<li><a href=\'".get_pagenum_link($paged + 1)."\'>$next</a></li>";
        if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages && false)
            echo "<li><a href=\'".get_pagenum_link($pages)."\'>&raquo;</a></li>";
        echo "</ul>\\n";
    }
}

function kriesi_pagination2($pages = \'\', $range = 1)
{
    $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+1 || $i <= $paged-$range-1) || $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";
    }
}
# +++++++++++++++++++++++++++++++Pagination ++++++++++++++++++++++++++++++++++++++
将以下代码放在您的列表页上

<?php $catquery = new WP_Query( \'post_type=article&posts_per_page=4&article-visibility=fitness\' );
while($catquery->have_posts()) : $catquery->the_post();
?>

   <article class="post home-post">
       <a href="<?php the_permalink(); ?>">
           <div class="post-thumbnail-img">
               <?php the_post_thumbnail(\'small-thumbnail\'); ?>
           </div>
           <h2><?php the_title(); ?></h2>
       </a>
       <p class="post-info">
           <?php the_time(\'F jS, Y\'); ?> |
           <?php get_template_part(\'block-athlete-archive\'); ?>
        </p>

       <p>
           <?php echo get_the_excerpt(); ?>
           <span class="readmore">
               <br><a href="<?php the_permalink(); ?>">Read more &raquo;</a>
           </span>
       </p>

   </article>

<?php endwhile; ?>`

<div class="pagination">
            <?php kriesi_pagination($catquery->max_num_pages); ?>
</div>
完成上述过程后,必须更新永久链接。

相关推荐

Can't Add Pagination

我无法添加分页来在页面之间划分帖子并在页面之间导航。目前所有20篇帖子我都有一次加载。我想在我的页面底部添加分页,并在5之前查看帖子。我有一个自定义的帖子类型,我将其定义为Project。我有两页。主页和日志。主页以我的项目帖子类型为特征,日志以我的常规博客帖子为特征。我希望这两页都有分页。我的当前索引。php是我的主页,如下所示:<?php get_header(); ?> <?php get_footer(); ?> <div class=\"p