Category foreach Paging

I'm using the following code to display categories on my homepage, what I'd like to do is add the ability for paging with previous and next links, I haven't been able to figure out how to do this with a foreach can anyone point me in the right direction?

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_IDorderby=randposts_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_IDorderby=nameposts_per_page=1"); ?
    h3a 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 : ?
    pI hate to break it to you...but right now there are strongno 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 } ?

Thanks,

Josh

Topic next previous php categories pagination Wordpress

Category Web


Here's some pagination I used for a foreach loop:

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 );

}

I figured it out!

After a lot of research and trial and error, I managed to figure out how to get the paging to work for both versions...my original code and the simplified code update.

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

The middle sections is where my original code is on both solutions, the top and bottom parts are where most of the changes have been made.

I found the solution here: http://erikeldridge.wordpress.com/2009/01/11/simple-php-paging/ - I just changed the array that he had and added the category array that I have, then configured the paging url and added a "(Page # of #)" to finish it up.

Everything works like a champ!

Thanks,
Josh

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.