0

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

Thanks,
Josh

  • WP pagination isn't going help in this way. You could just make your own using jQuery and count($categories) to get the total number of sub-categories and then work that into your foreach loop to (for example): display the first 6 subs and hide the rest; and, add pagination accordingly to hide the first 6, show next 6, etc.. make sense? Just an option. – deflime Jul 16 '14 at 17:58
  • 1
    Running multiple queries, and specially using query_posts is not going to help your cause here. I'm not going to say it is not doable with the proper functions and properplanning, but you are going to have a tough time getting this to work – Pieter Goosen Jul 16 '14 at 18:01
  • I have reworked the code and the updated code (shown above) is a lot easier to read and pretty much does the same thing (it's just missing the post image, but that's ok). How would I paginate that? Thanks – Josh Rodgers Jul 16 '14 at 20:21
2

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

-2

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

}
  • This looks great, I add this to my functions file right? How do I use it in my template? – Josh Rodgers Jul 16 '14 at 22:27
  • You would add that function to your functions file and then call it after your loop in your template. I haven't used the piece of code in a really long time, but as I remember I took it from: zurb.com/forrst/posts/PHP_Simple_pagination_function-vHK Hopefully this helps you more! – user3325126 Jul 16 '14 at 22:44
  • I wasn't able to figure out how to implement your solution, but I solved my problem. Looks like your solution is pretty similar...just wrapped up in a function instead of being added on the page. Thanks for the help :-) – Josh Rodgers Jul 17 '14 at 1:55
  • @user3325126 whay you don't paste the code here? Answer should not rely on external links. – gmazzap Jul 17 '14 at 3:36
  • @G.M. Sorry I'm newer to stackexchange and was having trouble making the code readable. Sorry I'm working on it! – user3325126 Jul 17 '14 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.