如何在我的代码中添加分页?

时间:2012-01-18 作者:markyeoj

我只是想问一下,如何在此代码中添加分页:

<?php if ( get_option(\'webly_display_media\') == \'on\' ){ ?>
                    <div id="recent-projects">
                        <div id="recent-projects-right">
                            <div id="recent-projects-content" class="clearfix">
                                <?php 
                                    $args=array(
                                        \'showposts\' => \'7\',
                                        \'category__not_in\' => get_option(\'webly_exlcats_media\')
                                    );
                                    query_posts($args);
                                ?>
                                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                                    <?php 
                                        $width = 140;
                                        $height = 72;
                                        $titletext = get_the_title();
                                        $thumbnail = get_thumbnail($width,$height,\'project-image\',$titletext,$titletext,true,\'Media\');
                                        $thumb = $thumbnail["thumb"];
                                        $et_medialink = get_post_meta($post->ID,\'et_medialink\',true) ? get_post_meta($post->ID,\'et_medialink\',true) : \'\';
                                        $et_videolink = get_post_meta($post->ID,\'et_videolink\',true) ? get_post_meta($post->ID,\'et_videolink\',true) : \'\';
                                    ?>
                                    <div class="project">
                                        <?php if ( $et_medialink <> \'\' ) { ?>
                                            <a href="<?php echo $et_medialink; ?>">
                                        <?php } elseif ( $et_videolink <> \'\' ) { ?>
                                            <a href="<?php echo $et_videolink; ?>" rel="prettyPhoto[media]" class="et-video" title="<?php echo $titletext; ?>">
                                        <?php } else { ?>
                                            <a href="<?php echo $thumbnail["fullpath"]; ?>" rel="prettyPhoto[media]" title="<?php echo $titletext; ?>">
                                        <?php } ?>
                                                <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, \'project-image\'); ?>
                                                <span class="zoom-icon"></span>
                                                <span class="project-overlay"></span>
                                            </a>
                                    </div>  <!-- end .project -->
                                <?php endwhile; ?>
                                <?php endif; wp_reset_query(); ?>

                            </div> <!-- end #recent-projects-content -->
                        </div> <!-- end #recent-projects-right -->
                    </div> <!-- end #recent-projects -->
我的帖子有一个特色视频,目前该代码根据类别显示5个特色视频。问题是,我在该类别中已经有11篇帖子,但主题只能显示5篇帖子,所以我想对此进行分页。你能帮帮我吗?这是开发网站http://dev.freelanceu.net/mjl/fitnessadventure/. 您可以在内容底部、页脚之前看到上述代码。

1 个回复
最合适的回答,由SO网友:AndrettiMilas 整理而成

将此添加到函数。php:

// Pagination
function pagination($pages = \'\', $range = 4)
{
     $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\\"><span>Page ".$paged." of ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>&laquo; First</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo; Previous</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)."\\">Next &rsaquo;</a>";
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>Last &raquo;</a>";
         echo "</div>\\n";
     }
}
然后使用以下方法获取分页链接:

<?php if (function_exists("pagination")) {
    pagination($loop->max_num_pages);
} ?>

结束

相关推荐

Keep Pagination in Tabs

有没有办法在多个选项卡上分页?我有一个页面有5个选项卡,每个选项卡都过滤了来自相同帖子的内容。例如:表1-所有帖子。选项卡2-带有关键字1的帖子。选项卡3-带有关键字2的帖子。等我的问题是,我不知道如何在每个选项卡上使用分页,也不知道如何让用户能够选择该选项卡上的页面并保持在该选项卡上-它想返回到第一个选项卡。