[wp-hackers] wp_list_categories()
Mike Schinkel
mikeschinkel at gmail.com
Fri Jan 2 00:32:04 GMT 2009
Also, it would be helpful to know where you got the list of categories and
posts?
get_categories()'s and get_posts()? If yes, what parameters did you pass to
get them?
-Mike
On Thu, Jan 1, 2009 at 1:21 PM, Daiv Mowbray <daiv at daivmowbray.com> wrote:
>
> On Jan 1, 2009, at 12:45 AM, Mike Schinkel wrote:
>
> That said, if Daiv Mowbray will post an example similar to the example I
>> posted of what he wants his markup to look like down to, say, the 4th
>> level
>> I'll write the recursive login for him. Just not tonight as I'm leaving
>> for
>> an NYE party soon. :-)
>>
>
> Hey Mike, hope the party was great!
> We had a family party here at home.
> Lots of fun with the kids.
>
> Re your offer...I'd really appreciate a hand with this, been going around
> in circles
> for a couple of weeks, If you're willing and able, I've added bellow the
> relevant functions.
> As for what I'm trying to do...
> I have an array of categories, and an array of posts .
>
> The desired result down to the 4th level is ;
> <dl>
> <dt class="dtL1">Cat level1</dt>
> <dd class="ddL1">
> <dl>
> <dt class="dtL2">Cat level2</dt>
> <dd class="ddL2">
> <dl>
> <dt class="dtL3">Cat level3</dt>
> <dd class="ddL3">
> <dl>
> <dt class="dtL4">Cat level4</dt>
> <dd class="ddL4"><span>post level4</span>
> </dd>
> </dl>
> <span>post level3</span>
> </dd>
> <dt class="dtL3">Cat level3-B</dt>
> <dd class="ddL3"><span>post level3-B</span>
> </dd>
> </dl>
> <span>post level2</span>
> </dd>
> </dl>
> <span>post level1</span>
> </dd>
> </dl>
>
>
> Here are the functions, I think I'm doing pretty well so far for a non
> phper.
>
> function getDepth( array &$items, $node, $depth = 0)
> {
> if ($node->parent == 0) {
> // Root node, return current depth
> return $depth;
> } else {
> // Search for matching parent node
> $parent = array_filter(
> $items,
> create_function(
> '$item',
> 'return ($item->term_id == ' .
> $node->parent . ');'
> )
> );
> $parent = array_pop($parent);
> return getDepth($items, $parent, ++$depth);
> }
> }
> function make_def_list($categories, $posts, $options ) {
> global $showPostCount, $linkToCat, $catfeed, $showPosts ;
>
> $parents =array();
>
> echo "\n<div id='ssMenuHolder'><dl id='ssMenuList'>\n";
>
>
> foreach( $categories as $cat ) {
> $my_depth = (getDepth( $categories, $cat) +1);
> $cat->depth = $my_depth;
>
> array_push ($parents , ($cat->parent) );
> if ($cat->depth == 1) // equals level 0 , Start top level
> {
> $rssLink=add_feedlink($catfeed,$cat);
> $sub_cat_post_count=0;
> $sub_cat_count=0;
>
> list
> ($sub_cat_links,$sub_cat_count,$sub_cat_post_count,$sub_cat_posts)=
> get_ssmsub_cat($cat, $categories, $parents, $posts,
> $sub_cat_count, $sub_cat_post_count, $number);
>
> $theCount=$cat->count;//$cat->count is num of posts
>
> cat_bar($cat, $showPostCount, $theCount, $rssLink);
>
> echo( "\n\t<dd class='showme_".$my_depth." ".$cat->slug."
> '>\n" );
>
> if ($sub_cat_count > 0)
> { // this top level cat has sub cats
> echo
> "\n\t<dl>\n".$sub_cat_links."\t\n</dl>\n ";
> }
>
> // Now print out the posts
> if( ! empty($posts) || ($showPosts=='yes'))
>
> add_posts($posts, $cat, $sub_cat_posts);
>
> echo "\t</dd><!-- ending first level category -->\n";
>
> }
> } // end if theCount>0
>
> echo "\n</dl></div>\n";
> }
> function get_ssmsub_cat($cat, $categories, $parents, $posts,
> $sub_cat_count, $sub_cat_post_count, $number) {
>
> global $showPostCount, $linkToCat, $catfeed, $showPosts ;
>
> $sub_cat_posts=array();
> $link2 = '';
>
> if (in_array($cat->term_id, $parents)) {
>
> foreach ($categories as $cat2) {
>
> $sub_cat_link2=''; // clear info from sub_cat_link2
>
> if ($cat->term_id == $cat2->parent)
> {
> // check to see if there are more
> subcategories under this one
>
> $sub_cat_post_count=$sub_cat_post_count+$cat2->count;
>
> if (!in_array($cat2->term_id,
> $parents)) {
>
> $sub_cat_count = 0;
>
> $sub_cat_links =
> cat_bar($cat2, $showPostCount, $theCount, $rssLink);
>
> } else {
> list ($sub_cat_link2,
> $sub_cat_count,$sub_cat_post_count,$sub_cat_posts)=
>
> get_ssmsub_cat($cat2, $categories, $parents, $posts, $sub_cat_count,
> $sub_cat_post_count, $number);
>
> $sub_cat_count=1;
>
> $sub_cat_links =
> cat_bar($cat2, $showPostCount, $theCount, $rssLink);
> }
> }
> $sub_cat_links.="\n\t<dd
> class='showme_".$my_depth."' >\n";
>
> if ($showPosts=='yes')
> {
> add_posts ($posts, $cat2, $sub_cat_posts);
>
> }
>
> $sub_cat_links.= "</dd><!-- ending subcategory dd -->\n";
> }
> }
> return
> array($sub_cat_links,$sub_cat_count,$sub_cat_post_count,$sub_cat_posts);
> }
> function cat_bar( $cat, $showPostCount, $theCount, $rssLink) {
> global $showPostCount, $linkToCat, $catfeed, $showPosts ;
>
>
> echo "\n\t<dt class='ssmToggleBar ".$cat->slug."'><span
> class='show_".$cat->depth." subsym'> </span>";
>
> $catbarlink = "\n\t\t<span class='catlink'><a
> href='".get_category_link($cat->term_id)."' class='tool' title='".
> sprintf(__("View all entries filed under %s"),
> wp_specialchars($cat->name))."'";
>
> if ( !empty($cat->description) )
> {
> $catbarlink .= 'rel="' .
> wp_specialchars(apply_filters('description',$cat->description,$cat)) . '"';
> }
>
> $catbarlink .= '>'.apply_filters('list_cats', $cat->name,
> $cat).'</a></span>';
>
> if( $showPostCount=='yes')
> {
> $catbarlink .= " (" .
> $theCount.")".$rssLink."\n\t</dt><!--this is catbar with post count -->";
>
> } else {
>
> $catbarlink .= $rssLink."\n\t</dt><!--this
> is catbar no post count -->";
> }
> echo $catbarlink ;
>
> }
> function add_posts ($posts, $cat, $sub_cat_posts) {
>
> foreach ($posts as $post) {
>
> if ($post->term_id == $cat->term_id) {
>
>
> echo "\n\t<span class='ssMenuPost'>\n\t<a
> href='".
>
> get_permalink($post->id)."'>".strip_tags($post->post_title) .
> "</a></span>\n";
> }
> if (!in_array($post->id, $sub_cat_posts)) {
> array_push($sub_cat_posts, $post->id);
> }
> }
> }
> ----------
> Daiv Mowbray
> daiv at daivmowbray.com
>
> ----------
>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list