viewing source for "fancy-categories-list.php"

last commit

commit 805ccb8a46abe077ea283c4c2af4a702e4855005 Author: andy <andy@a04a6e75-5819-0410-a511-8dafea38c924> Date: Wed Dec 12 18:15:36 2007 +0000
Added functionality: when a single post is displayed the categories of that post will automatically be expanded

source code

001: <?php
002: /*
003: Copyright 2007 Andrew Rader
004: 
005: This file is part of Fancy Categories
006: 
007: Fancy Categories is free software; you can redistribute it and/or modify
008: it under the terms of the GNU General Public License as published by
009: the Free Software Foundation; either version 2 of the License, or
010: (at your option) any later version.
011: 
012: Fancy Categories is distributed in the hope that it will be useful,
013: but WITHOUT ANY WARRANTY; without even the implied warranty of
014: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015: GNU General Public License for more details.
016: 
017: You should have received a copy of the GNU General Public License
018: along with Fancy Categories; if not, write to the Free Software
019: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020: */
021: 
022: // Helper functions
023: 
024: function get_category_total($cat) {
025:     $total = 0;
026:     $subs = array();
027:     $curr_cat = $cat;
028: 
029:     do {
030:         $total += $curr_cat->category_count;
031: 
032:         $cats = get_categories( "child_of=$cat->cat_ID" );
033:         foreach( $cats as $c ) {
034:             if( $c->category_parent == $curr_cat->cat_ID ) {
035:                 array_push( $subs, $c );
036:             }
037:         }
038:         /*
039:         $subs = array_merge( $subs, $cats );
040:         */
041:         $curr_cat = array_pop( $subs );
042:     } while( $curr_cat != NULL );
043: 
044:     return $total;
045: }
046: 
047: function print_category( $cat, $expanded ) {
048:     if( ! in_array( $cat->cat_ID, $expanded ) ) {
049:         $style = 'display:none;';
050:         $rel = 'show';
051:     }
052:     else {
053:         $style = 'display:;';
054:         $rel = 'hide';
055:     }
056: 
057:     echo "<li>\n";
058:     
059:     $categories = get_categories( "child_of=$cat->cat_ID" );
060: 
061:     $posts = get_posts( "numberposts=0&category=$cat->cat_ID" );
062: 
063:     $link = '<a id="fancycat_link_'.$cat->cat_ID.'" href="'.get_category_link($cat->cat_ID).'" ';
064:     $link .= 'onclick="fancat_click( event );" rel="'.$rel.'">';
065:     $link .= apply_filters('list_cats', $cat->cat_name, $cat).'</a>';
066: 
067:     if( fancat::get_show_post_count() ) {
068:         if( fancat::get_show_count_total() ) {
069:             $count = get_category_total($cat);
070:             $link .= " ($count)";
071:         }
072:         else {
073:             $link .= ' ('.intval($cat->category_count).')';
074:         }
075:     }
076: 
077:     echo $link;
078: 
079:     if( ! empty($categories) ) {
080:         echo "\n<ul style=\"$style\" id=\"fancycat_sub_$cat->cat_ID\">\n";
081:         foreach( $categories as $c ) {
082:             if( $c->category_parent == $cat->cat_ID ) {
083:                 print_category( $c, $expanded );
084:             }
085:         }
086:         echo "\n</ul>\n";
087:     }
088: 
089:     echo "<ul id=\"fancycat_posts_$cat->cat_ID\" style=\"$style\">\n<li>";
090:     _e('Posts');
091:     echo ":\n<ul>\n";
092: 
093:     $text = fancat::get_post_string();
094:     foreach( $posts as $post ) {
095:         $perm = get_permalink( $post->ID );
096: 
097:         $trim = fancat::get_title_size();
098:         if( $trim > 0 ) {
099:             $title = substr( $post->post_title, 0, $trim );
100:             if( fancat::get_show_ellipsis() 
101:                     && strlen($post->post_title) > $trim ) {
102:                 $title .= ' ...';
103:             }
104:         }
105:         else {
106:             $title = $post->post_title;
107:         }
108: 
109:         $t = str_replace( '%title', $title, $text );
110:         $t = str_replace( '%id', $post->ID, $t );
111:         echo "<li><a href=\"$perm\">$t</a></li>\n";
112:     }
113:     echo "</ul></li>\n</ul>\n</li>\n";
114: }
115: ?>
116: 
117: <ul id="fancy_cats" class="fancy_cats">
118: <?php
119:     global $wp_query;
120:     global $post;
121: 
122:     $categories = get_categories();
123:     $expanded = array();
124: 
125:     if( is_category() ) {
126:         $curr_cat = $wp_query->get_queried_object();
127:         $tmp_id = $curr_cat->cat_ID;
128:         do {
129:             array_push( $expanded, $tmp_id );
130:             $curr_cat = get_category( $curr_cat->category_parent );
131:             $tmp_id = $curr_cat->cat_ID;
132:         } while( $tmp_id != 0 );
133:     }
134:     else if( is_single() ) {
135:         $post_cats = get_the_category($post->ID);
136: 
137:         foreach( $post_cats as $curr_cat ) {
138:             $tmp_id = $curr_cat->cat_ID;
139:             do {
140:                 array_push( $expanded, $tmp_id );
141:                 $curr_cat = get_category( $curr_cat->category_parent );
142:                 $tmp_id = $curr_cat->cat_ID;
143:             } while( $tmp_id != 0 );
144:         }
145:     }
146: 
147:     foreach( $categories as $cat ) {
148:         if( $cat->category_parent == 0 ) {
149:             print_category( $cat, $expanded );
150:         }
151:     }
152: ?>
153: </ul>