WordPress.org

Ready to get started?Download WordPress

Forums

My gift to you: Category Listing with current category displaying post titles (3 posts)

  1. Doodlebee
    Member
    Posted 5 years ago #

    I thought I would come in here and share this. This was such an unusual request, and so unique and interesting, that I couldn't NOT share it. It took me all day to figure this out and code it (and I'm still willing to bet it could be coded more cleanly) and I used bits and pieces of code I found here on the forums and in the Codex.

    So here's the deal. Client wanted me to have a sidebar that had the following criteria:

    1) when you were on any category listing, it would list links to the subcategories of that parent.
    2) that had to work for all sub-categories (grandchildren, great-grandchildren...) too. The topmost parent category would show, with a list of sub-, sub-sub-, (on and on) links.

    both of these could be done with wp_list_categories but the kicker was this: if you were on a single post page, it still had to do it AND it had to list all the post titles only in that category. Meaning, it couldn't list parent titles, not children. Only ones in that specific category.

    I have to say, there was a point where I thought I wouldn't be able to do it... but I did. So here you go.

    function list_page_children() {
    global $post, $wp_query;
    $cat = $wp_query->get_queried_object();
    	if(is_category()) {
    	 	$id = $cat->term_id;
    	 	$current_name = $cat->slug;
    
    	 	if($id > '0') {
    	 		$slugs = get_category_parents($cat, FALSE, ',', TRUE);
    	 		$getparentslugs = explode(',',$slugs);
    	 		$slug = $getparentslugs[0];	 
    
    	 		$names = get_category_parents($cat, FALSE, ',', FALSE);
    	 		$getparentnames = explode(',',$names);
    	 		$parent	= $getparentnames[0];
    
    	 		$getparentid = get_category_by_slug($slug);
      			$parentid = $getparentid->term_id;
    	 	}	 	
    
    	 	 $thelist = wp_list_categories('title_li=&echo=0&child_of=' . $parentid) . "\n";
    
    	} else if(is_single()) {
    		$category = get_the_category();
    		$parent = $category[0]->cat_name;
    		$postcatid = $category[0]->term_id;
    		$postcatcount = $category[0]->count;
    
    		if($postcatid > '0') {
    			$slugs = get_category_parents($postcatid, FALSE, ',', TRUE);
    	 		$getparentslugs = explode(',',$slugs);
    	 		$slug = $getparentslugs[0];	 
    
    	 		$names = get_category_parents($postcatid, FALSE, ',', FALSE);
    	 		$getparentnames = explode(',',$names);
    	 		$parent	= $getparentnames[0];
    
    	 		$getparentid = get_category_by_slug($slug);
      			$parentid = $getparentid->term_id;
    
    		}	
    
            $getsubs = get_categories('child_of=' . $parentid);
            if($getsubs) {
            	$categories = $getsubs;
            	$isparent = 'no';
            } else {
            	$categories = get_the_category();
            	$isparent = 'yes';
            }
    
    		if($categories) {
    		  foreach($categories as $cat) {
    		  	if ($cat->count > '0') {
    		  	  if($isparent != 'yes') {
    		      $thelist .= '<li><a href="' . get_category_link( $cat->term_id) . '">' . $cat->cat_name . '</a>' . "\n";
    		  	  }
    		      $numposts = -1;
    		      $posts = get_posts('cat='. $cat->term_id . '&showposts=' . $numposts);
    		      if($posts) {
    		      	if($cat->term_id == $postcatid) {
    		      	$thelist .= '<ul>' . "\n";
                    foreach($posts as $post) {
                      setup_postdata($post);
                      $thelist .= '<li><a href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
                    }
                    $thelist .= '</ul>' . "\n";
    		      	}
    		      }
    		      $thelist .= '</li>' . "\n";
                }
              }
    		}
    	}
    
    	$empty = 'No categories';
    	$find = strpos($thelist, $empty);
    	if($find != '') {
    		$thelist = '';
    	} 
    
    ?>
    
    <h3><?php echo $parent; ?></h3>
      <ul id="children">
        <?php echo $thelist; ?>
      </ul>  
    
    <?php
    }

    Like I said, some of the code is redundant - probably could use some cleaning up. If anyone wants to take that on, more power to ya - but his works a treat. I'm all proud of myself.

    Hope it helps someone :)

  2. Christina Lannen
    Member
    Posted 5 years ago #

    We love you Shelly!

    Chris

  3. Doodlebee
    Member
    Posted 5 years ago #

    Kay -already have fixes. If you were in a a category that had child categories, then it would list the subcategory posts as part of the current category you're in. There was also no classes set to determine if you were in a current category or not (for styling purposes). I fixed both these issues with the following changes...

    find this part:

    foreach($posts as $post) {
                      setup_postdata($post);
                      $thelist .= '<li><a href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
                    }

    and replace it with this:

    foreach($posts as $post) {
                      setup_postdata($post);
                      $postlink = get_the_ID();
                      if($postlink == $thispost) {
                      	$linkclass = ' class="current-post"';
                      } else {
                      	$linkclass = '';
                      }
                      if(in_category($cat)) {
                      $thelist .= '<li class="' . $class . '"><a' . $linkclass . ' href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
                      }
                    }

    and there you go :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.