[wp-hackers] Improved template-functions-category.php

Allan Mertner amwp at mertner.com
Sun Sep 12 12:45:24 UTC 2004


I would like to suggest an improvement to some of the code in 
template-functions-category.php, allowing greater flexibility and 
styling.  The change involved 3 things:
 - Don't just store the lastday and lastmonth for each category, but 
encode the date and store it as a PHP timestamp,
 - Use the date format setting to style the last date, rather than 
hardcode the format,
 - Add div elements with class tags to each category item, item count 
and date item in the category list.

Without these changes, the HTML output for a category looked like this:

<li><a href="http://site/index.php?cat=11" title="Test">Test</a> (1) 16/8
</li>

With the proposed changes, the output could be (extra linebreaks added 
for readability):

<li><div class="cat_item">
  <a href="http://site/index.php?cat=11" title="Test">Test</a>
  <div class="cat_count"> (1)</div> <div class="cat_date">12/08/00</div></div>
</li>

- which allows for nice separate styling of the count and date items.  
For existing users, the only difference will be that their 
last-posted-dates will be shown in their desired format rather than as 
hardcoded "day/month".

The code changes are detailed in this diff from CVS:

Index: template-functions-category.php
===================================================================
RCS file: 
/cvsroot/cafelog/wordpress/wp-includes/template-functions-category.php,v
retrieving revision 1.39
diff -r1.39 template-functions-category.php
340,354c340,357
<               foreach ($cat_dates as $cat_date) {
<                       $category_lastday["$cat_date->cat_ID"] = 
$cat_date->lastday;
<                       $category_lastmonth["$cat_date->cat_ID"] = 
$cat_date->lastmonth;
<               }
<       }
<
<       if (intval($optionall) == 1 && !$child_of && $categories) {
<               $all = apply_filters('list_cats', $all);
<               $link = "<a 
href=\"".$file.$querystring_start.'cat'.$querystring_equal.'all">'.$all."</a>";
<               if ($list) {
<                       echo "\n\t<li>$link</li>";
<               } else {
<                       echo "\t$link<br />\n";
<               }
<       }
---
 >         if (! empty($cat_dates)) {
 >             foreach ($cat_dates as $cat_date) {
 >                 $lastdate = mktime(0, 0, 0, $cat_date->lastmonth, 
$cat_date->lastday, $cat_date->lastyear);
 >                 $category_last["$cat_date->cat_ID"] = $lastdate;
 >             }
 >         }
 >       }
 >
 >     if (intval($optionall) == 1 && !$child_of && $categories) {
 >         $all = apply_filters('list_cats', $all);
 >         $link = "<a 
href=\"".$file.$querystring_start.'cat'.$querystring_equal.'all">'.$all."</a>";
 >         $link = "<div class='cat_item'>" . $link . "</div>";
 >         if ($list) {
 >             echo "\n\t<li>$link</li>";
 >         } else {
 >             echo "\t$link<br />\n";
 >         }
 >     }
404c407
<                               $link .= ' 
('.intval($category_posts["$category->cat_ID"]).')';
---
 >                 $link .= '<div class="cat_count"> 
('.intval($category_posts["$category->cat_ID"]).')</div>';
407c410,412
<                               $link .= ' 
'.$category_lastday["$category->cat_ID"].'/'.$category_lastmonth["$category->cat_ID"];
---
 >                 $link .= ' <div class="cat_date">';
 >                 $link .= date( get_settings('date_format'), 
$category_last["$category->cat_ID"] );
 >                 $link .= '</div>';
410c415,416
<                               $thelist .= "\t<li>$link\n";
---
 >                 $link = "<div class='cat_item'>" . $link . "</div>";
 >                 $thelist .= "\t<li>$link\n";
451c457
< ?>
\ No newline at end of file
---
 > ?>

Allan






More information about the hackers mailing list