Category foreach Paging

时间:2014-07-16 作者:Josh Rodgers

我正在使用以下代码在我的主页上显示类别,我想做的是添加上一个和下一个链接的分页功能,我还不知道如何使用foreach 有人能给我指出正确的方向吗?

Original Code:

<?php 
    $cat = get_cat_ID("Photos");
    $categories = get_categories("child_of=$cat");
?>
<?php if (is_array($categories) && count($categories) > 0) : ?>
    <?php foreach ($categories as $category) { ?>
        <?php query_posts("cat=$category->cat_ID&orderby=rand&posts_per_page=1"); ?>
            <?php while (have_posts()) : the_post(); ?>
                <a href="<?php bloginfo(\'url\'); ?>?cat=<?php echo $category->cat_ID; ?>"><?php the_post_thumbnail("gallery-thumb"); ?></a>
            <?php endwhile; ?>
        <?php wp_reset_query(); ?>
    <?php query_posts("cat=$category->cat_ID&orderby=name&posts_per_page=1"); ?>
    <h3><a href="<?php bloginfo(\'url\'); ?>?cat=<?php echo $category->cat_ID; ?>"><?php single_cat_title(); ?></a></h3>
    <?php wp_reset_query(); ?>
    <?php $query = $category->count; ?>
    <?php if ($query == 1) $query .= " Image"; else $query .=" Images"; echo \'<p class="center">(\'.$query.\')</p>\'; //Album Count ?>
<?php } ?>
<?php else : ?>
    <p>I hate to break it to you...but right now there are <strong>no albums available</strong> for viewing, please try again later.</p>
<?php endif; ?>

Code Update:

<?php
    $parent = get_cat_ID("Photos");
    $cats = get_categories("child_of=$parent");
?>
<?php foreach ($cats as $cat) { ?>
    <?php echo $cat->name; ?>
    <?php $query = $cat->count; ?>
    <?php if ($query == 1) $query .= " Image"; else $query .=" Images"; echo \'<p class="center">(\'.$query.\')</p>\'; //Album Count ?>
<?php } ?>
谢谢,乔希

2 个回复
最合适的回答,由SO网友:Josh Rodgers 整理而成

我想出来了!

经过大量的研究和反复尝试,我终于想出了如何让分页在两个版本中都能正常工作。。。我的原始代码和简化代码更新。

Original Code Solution:

<?php
    $cat = get_cat_ID("Photos");
    $categories = get_categories("child_of=$cat");

    $limit = 9;

    $total = count($categories);
    $pages = ceil($total / $limit);
    $result = ceil($total / $limit);

    $current = isset($_GET[\'paged\']) ? $_GET[\'paged\'] : 1;
    $next = $current < $pages ? $current + 1 : null;
    $previous = $current > 1 ? $current - 1 : null;

    $offset = ($current - 1) * $limit;
    $categories = array_slice($categories, $offset, $limit);
?>

<?php if (is_array($categories) && count($categories) > 0) : ?>
    <?php foreach ($categories as $category) { ?>
        <?php query_posts("cat=$category->cat_ID&orderby=rand&posts_per_page=1"); ?>
            <?php while (have_posts()) : the_post(); ?>
                <a href="<?php bloginfo(\'url\'); ?>?cat=<?php echo $category->cat_ID; ?>"><?php the_post_thumbnail("gallery-thumb"); ?></a>
            <?php endwhile; ?>
        <?php wp_reset_query(); ?>
        <?php query_posts("cat=$category->cat_ID&orderby=name&posts_per_page=1"); ?>
            <h3><a href="<?php bloginfo(\'url\'); ?>?cat=<?php echo $category->cat_ID; ?>"><?php single_cat_title(); ?></a></h3>
        <?php wp_reset_query(); ?>
        <?php $query = $category->count; ?>
        <?php if ($query == 1) $query .= " Image"; else $query .=" Images"; echo \'<p class="center">(\'.$query.\')</p>\'; //Album Count ?>
    <?php } ?>
    <?php else : ?>
        <p>I hate to break it to you...but right now there are <strong>no albums available</strong> for viewing, please try again later.</p>
    <?php endif; ?>

    <?php echo "<p>(Page: ". $current . " of " . $result .")</p>"; ?>
        <? if($previous): ?>
            <a href="<?php bloginfo(\'url\'); ?>?paged=<?= $previous ?>">Previous</a>
        <? endif ?>
        <? if($next) : ?>
            <a href="<?php bloginfo(\'url\'); ?>?paged=<?= $next ?>">Next</a>
        <? endif ?>

Code Update Solution:

<?php
    $parent = get_cat_ID("Photos");
    $cats = get_categories("child_of=$parent");

    $limit = 9;

    $total = count($cats);
    $pages = ceil($total / $limit);
    $result = ceil($total / $limit);

    $current = isset($_GET[\'paged\']) ? $_GET[\'paged\'] : 1;
    $next = $current < $pages ? $current + 1 : null;
    $previous = $current > 1 ? $current - 1 : null;

    $offset = ($current - 1) * $limit;
    $cats = array_slice($cats, $offset, $limit);
?>

<?php foreach ($cats as $cat) { ?>
    <?php echo $cat->name; ?>
    <?php $query = $cat->count; ?>
    <?php if ($query == 1) $query .= " Image"; else $query .=" Images"; echo \'<p class="center">(\'.$query.\')</p>\'; //Album Count ?>
<?php } ?>

<?php echo "<p>(Page: ". $current . " of " . $result .")</p>"; ?>
<? if($previous): ?>
    <a href="<?php bloginfo(\'url\'); ?>?paged=<?= $previous ?>">Previous</a>
<? endif ?>
<? if($next) : ?>
    <a href="<?php bloginfo(\'url\'); ?>?paged=<?= $next ?>">Next</a>
<? endif ?>
中间部分是我的原始代码在这两个解决方案上的位置,顶部和底部是大部分更改的位置。

我在这里找到了解决方案:http://erikeldridge.wordpress.com/2009/01/11/simple-php-paging/ - 我只是更改了他拥有的数组并添加了我拥有的类别数组,然后配置了分页url并添加了“(第#页,共#)页”来完成它。

一切都像冠军一样!

谢谢,乔希

SO网友:user3325126

下面是我用于foreach循环的一些分页:

function foreach_pagination( $current, $pages, $link = \'?page=%d\', $tag = \'ul\', $item_tag = \'li\' )
{
  $min = ( $current - 3 < $pages && $current - 3 > 0 ) ? $current - 3 : 1;
  $max = ( $current + 3 > $pages ) ? $pages : $current + 3;

  $output = array();

  for( $i = $min; $i <= $max; $i++ )
  {
    if($current == $i)
      $output[] = "<span class=\'active\'>{$i}</span>";
    else
      $output[] = \'<a href="\'.sprintf($link,$i).\'">\' . $i . \'</a>\';
  }

  if ( $current + 1 < $pages )
    $output[] = \'<a href="\' . sprintf( $link, $current + 1 ) . \'">Next</a>\';

  if ( $current - 1 > 0 )
     array_unshift($output, \'<a href="\'.sprintf($link,$current-1).\'">Previous</a>\');


  $li = \'\';
  foreach( $output as $link ):  
      $li .= sprintf( \'<%s>%s</%s>\', $item_tag, $link, $item_tag );
  endforeach;

  return sprintf( \'<%s>%s</%s>\', $tag, $li, $tag );

}

结束

相关推荐

Pagination custom query

我正在尝试制作一个自定义页面模板,以显示浏览次数最多的帖子。我可以回复帖子,但我很难弄清楚如何分页。以下是我所拥有的:$args = array(\'orderby\' => \'meta_value_num\', \'meta_key\' => \'post_views_count\', \'posts_per_page\' => 36 ); $loop = new WP_Query( $args ); while ( $