Monthly Archives: June 2010

List categories by letter

The code below is for a template for the WP 2.9 default theme that lists categories by the first letter of the category name.

An updated version of this code, with the capability to list terms of any taxonomy is shown in this post.

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */
/*
Template Name: listcatsbyletter
*/
?>
<?php /*
 Author: Mac McDonald
 Contact at BluegrassMiataClub.com using About->Contact Us form.

 This program creates a list of categories as links to the archive for
 each category.

 Set $hide_uncategorized = false; to show the uncategorized category.
     $hide_description = true; to hide the description of each category.
     $limit = the number of categories (not including Letters) per page.
     $range = number of page links in the nav bar. (5 is a good number).
*/?>

<?php get_header(); ?>

<?php
$hide_uncategorized = true;
$hide_description = false;
$limit = 25;  // Show this many entries per page
$range = 5;  // Show this many nav bar page links (other than first & last).
$args = array('orderby' => 'name', 'order' => 'ASC');
if ($hide_uncategorized) $args['exclude'] = '1';
$categories = get_categories($args);
if ($categories) :
   // For some unknown reason, $categories has no [0] entry at times, so renumber.
   $categories = array_merge($categories,array());
   if (isset($_GET['page'])) {
      $page = $_GET['page'];
   } else {
      $page = 1;
   }
   $start = ($page - 1) * $limit;
   $currletter = '';
   echo '<ul class="category_list">';
   for ($i=$start;$i<($start + $limit);++$i) {
      if ($i < sizeof($categories)) {
         $cat =  $categories[$i];
         $catletter = strtoupper(substr($cat->name,0,1));
         if ($currletter != $catletter) {
            if ($currletter != '') echo '</ul>';
            $currletter = $catletter;
            echo "<h2> - $catletter - </h2>";
            echo '<ul>';
         }
         echo '<li class="category_entry">Category: <a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a> </p> ';
         if (!$hide_description) echo '<p>Description: '. $cat->description . '</p>';
         echo '</li>';
      }
   }
   echo '<br /><br />';
   echo _cat_paginate(sizeof($categories),$limit,$range);
   echo '</ul>';
else:
   echo '<h2></h2>';
   echo 'There are no Categories to list';
endif;?>

<?php get_footer(); ?>

<?php function _cat_paginate($numrows,$limit=10,$range=7) {

   $pagelinks = "<div class=\"pagelinks\">";
   $currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
   if ($numrows > $limit) {
      if(isset($_GET['page'])){
         $page = $_GET['page'];
      } else {
         $page = 1;
      }
      $currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
      $currpage = str_replace("&page=".$page,"",$currpage);
      if($page == 1){
         $pagelinks .= "<span class=\"pageprevdead\">&laquo PREV </span>";
      }else{
         $pageprev = $page - 1;
         $pagelinks .= "<a class=\"pageprevlink\" href=\"" . $currpage .
               "&page=" . $pageprev . "\">&laquo PREV </a>";
      }
      $numofpages = ceil($numrows / $limit);
      if ($range == "" or $range == 0) $range = 7;
      $lrange = max(1,$page-(($range-1)/2));
      $rrange = min($numofpages,$page+(($range-1)/2));
      if (($rrange - $lrange) < ($range - 1)) {
         if ($lrange == 1) {
            $rrange = min($lrange + ($range-1), $numofpages);
         } else {
            $lrange = max($rrange - ($range-1), 0);
         }
      }
      if ($lrange > 1) {
         $pagelinks .= "<a class=\"pagenumlink\" " .
            "href=\"" . $currpage . "&page=" . 1 .
            "\"> [1] </a>";
         if ($lrange > 2) $pagelinks .= "&nbsp;...&nbsp;";
      } else {
         $pagelinks .= "&nbsp;&nbsp;";
      }
      for($i = 1; $i <= $numofpages; $i++){
         if ($i == $page) {
            $pagelinks .= "<span class=\"pagenumon\"> [$i] </span>";
         } else {
            if ($lrange <= $i and $i <= $rrange) {
               $pagelinks .= "<a class=\"pagenumlink\" " .
                        "href=\"" . $currpage . "&page=" . $i .
                        "\"> [" . $i . "] </a>";
            }
         }
      }
      if ($rrange < $numofpages) {
         if ($rrange < $numofpages - 1) $pagelinks .= "&nbsp;...&nbsp;";
            $pagelinks .= "<a class=\"pagenumlink\" " .
               "href=\"" . $currpage . "&page=" . $numofpages .
               "\"> [" . $numofpages . "] </a>";
      } else {
         $pagelinks .= "&nbsp;&nbsp;";
      }
      if(($numrows - ($limit * $page)) > 0){
         $pagenext = $page + 1;
         $pagelinks .= "<a class=\"pagenextlink\" href=\"" . $currpage .
                    "&page=" . $pagenext . "\"> NEXT &raquo;</a>";
      } else {
         $pagelinks .= "<span class=\"pagenextdead\"> NEXT &raquo;</span>";
      }

   }
$pagelinks .= "</div>";
return $pagelinks;
}
?>

Mashable article: How to get up to date on 3.0

Mashable article: How to get up to date on 3.0

A Sticky Post Orderer Plugin

This plugin lets you order sticky posts on category and tag archive pages.

Download the plugin here.

video on using the plugin

Stay in Category

The text of this article is also in a pastebin – http://pastebin.com/4fLC9FLy

In many themes, the previous/next_post_link in single.php does not stay in category. If you view a category archive, then select a single post, the prev/next link may not show a post in the same category.

If you set the ‘in_same_cat’ parameter to the previous/next_post_link calls in single.php to ‘true’, single.php will stay in category provided the posts belong only to one category. If posts belong to more than one category, the prev/next links may point to posts in one of the categories other than the one from the category archive. Changing ‘in_same_cat’ will also affect how a blog works, because you normally don’t want to stay in category when coming from a blog post list.
Continue reading