0

I recently added thumbnails for my categories, which has had the side effect of making the page they are listed on unbearably long. Because of that, I am hoping to paginate the list of categories.

Here is what I am using to generate the list of categories and thumbnails (unstyled):

<?php
 $args=array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories=get_categories('child_of=504&order_by=name&style=none&title_li=');
  foreach($categories as $category) { 
  echo '
  <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '> '. get_the_term_thumbnail ( $category->term_id, category, $size = 'medium', $attr = '') . ' </a>
  <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
  } 
?>

In trying to find a solution, I came across another question ( How to paginate a list of tags ) regarding tags that seems to do exactly what I am hoping, but have been unsuccessful merging the two together so far.

Any help would be wonderful, Thank you

6

Untested, but this should at least be on the right track:

$posts_per_page = 50;

$page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset = ( $page - 1 );

$args = array(
    'child_of' => 504,
    'order_by' => 'name',
);
$categories = get_categories( $args );

for( $i = $offset * $posts_per_page; $i < ( $offset + 1 ) * $posts_per_page; $i++ ) {
    $category = $categories[$i];
    echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '> '. get_the_term_thumbnail ( $category->term_id, category, $size = 'medium', $attr = '') . ' </a><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
}
unset( $category );

secondary pagination method

if( strpos( $_SERVER['REQUEST_URI'], '/page/' ) !== false ) {
    $uri = explode( '/', $_SERVER['REQUEST_URI'] );
    foreach ( $uri as $k => $v ) {
        if ( $value == "" )
            unset( $uri[$k] );
    }
    $offset = ( array_pop($uri) * $posts_per_page ) - $posts_per_page;
}

That will check the URL for /page/, if it contains /page/ it will strip all the empty values out and set the offset to the final value of the array. It's not the most elegant solution, but it works and its good on processing power

  • Thanks! When I drop that code into the site, it works perfectly for limiting the categories shown to 50. However, when I combine it with the pagination code from the link provided above, I am getting division by zero errors after each category. – Stephen S. Jan 27 '12 at 16:11
  • Try setting get_query_var( 'page' ) instead of paged (both places) I think that should do it. The pagination is already built in, so don't try to combine them, just use this. There's no division in my code, so it' something your doing, not something I wrote that's throwing the error. – mor7ifer Jan 27 '12 at 16:29
  • It displays 50 categories just fine, but I am not seeing any way to go to the next 50. Should I need additional code for that? – Stephen S. Jan 27 '12 at 16:38
  • Try typing the link in directly {your current url}/page/2, the functions you should be looking at are things like wp_link_pages() and posts_nav_link(), it sorta depends on how your WP install is setup as to which will work. – mor7ifer Jan 27 '12 at 17:13
  • 1
    Use the offset to generate the link. Previous page = {url}/page/$offset; next page = {url}/page/($offset+2). Filter for offset = 1 (on first page) and for count($categories)/$offset < 50 (on last page) and don't show the links at that point. If you need help on that, you can probably just start a new question. – mor7ifer Jan 28 '12 at 0:02

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.