WordPress.org

Ready to get started?Download WordPress

Forums

Link from a category view to next category? (2 posts)

  1. hilj
    Member
    Posted 4 years ago #

    I have some categories like 2007, 2008, 2009... The categories hold the events that took place that year.

    When I'm browsing the category 2009, I would like to have an arrow ← that you can click to get to the next/previous category.

    Can it be done?

    Thanks for all the help!

  2. MichaelH
    Member
    Posted 4 years ago #

    Didn't test this much but could try:

    <?php
    // for use in category template, get link to previous/next (alphabetic) categories
    if ( is_category() ) {
      $current_cat = get_query_var('cat');
      $previous = -1;
      $next = 0;
      $count = 0;
      $args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'hierarchical' => 0,
        'hide_empty' => 1
        );
      $categories = get_categories($args);
      foreach ($categories as $cat) {
        $count++;
        if ($cat->cat_ID == $current_cat) {
          $previous = $count - 2;
          $next = $count;
        }
      }
      if ($previous >= 0) {
        echo '<p>Previous category is: <a href="' . get_category_link( $categories[$previous]->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $categories[$previous]->name ) . '" ' . '>' . $categories[$previous]->name.'</a> </p> ';
      }
      if ($next > 0 && $next < count($categories)) {
        echo '<p>Next category is: <a href="' . get_category_link( $categories[$next]->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $categories[$next]->name ) . '" ' . '>' . $categories[$next]->name.'</a> </p> ';
      }
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic