WordPress jh_random_cats()

Got bored and scratched another itch I’ve had in wordpress for a while. I always wanted a way to list my categories on the sidebar on my terms. The best I had found was the well written and executed Category Visibility-RH but I wanted to choose from all my categories and still limit the number listed.

I added a function to my wp-hacks.php and then dropped it into my sidebar. Does the job, and could easily be edited. I swear someday I’ll learn how to make plug-ins, really.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function jh_random_cats($howMany) {
    global $wpdb ;
 
		$query = "
			SELECT cat_ID, cat_name, category_count
			FROM $wpdb->categories
      WHERE category_count != '0'
    ";
		$categories = $wpdb->get_results($query);
 
		shuffle($categories); // Mix 'em up
 
		for($howMany; $howMany > 0; $howMany--) {
      $poppedCat = array_pop($categories);
      print '<li><a href="'.get_category_link($poppedCat->cat_ID).'">'.$poppedCat->cat_name.'</a> ('.$poppedCat->category_count.')</li>';
    }
}

As a super-cool extra I got to use a PHP function I had never gotten to use before, let alone knew existed: bool shuffle ( array &array ). It, you guessed it, shuffles up an array.

Posted February 5th, 2007 - Permalink
Categories: PHP - Programming - Snippets - Wordpress
No Comments »
 
WordPress: more_posts, old_posts, new_posts

I just scratched an itch I’ve had for a long time. I always hate how on my category pages and my archive pages the little “More Posts” box would display even if there were no posts. Also, I could never check and see if there were no “Newer” or no “Older” posts so I could still print the words out in non-link form.

I finally did something about it today. I started by looking through the codex, and finding nothing. So I started digging through the WP code looking for the posts_nav_link() which sorta did what I wanted, but just echoed to the page instead of returning. I found it in wp-includes/template-functions-links.php and started chopping it up.

In the end I came up with three functions for my ‘my-hacks.php’ file. I didn’t want to spend the time making it into a plugin since it’s not that big a deal. They aren’t terribly efficient, but they work and thats what matters. Also, I pre-pended jh to the function names so there wouldn’t be any chance of collisions.

Returns true if there are more posts, newer or older.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function jh_more_posts() {
  global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages;
	if ( isset($max_num_pages) ) {
		$max_page = $max_num_pages;
	} else {
		preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
		$fromwhere = $matches[1];
		$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
		$max_page = $max_num_pages = ceil($numposts / $posts_per_page);
	}
	if( !$paged )
	  $paged = 1;
	if($paged == 1 && $paged == $max_page)
	  return false;
	else
	  return true;	
}

Returns true if there are more ‘older’ posts.

1
2
3
4
5
6
7
function jh_more_old() {
  global $paged;
	if( !$paged  || $paged == 1)
	  return true;	
	 else
	  return false;
}

Returns true if there are more ‘newer’ posts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function jh_more_new() {
  global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages;
	if ( isset($max_num_pages) ) {
		$max_page = $max_num_pages;
	} else {
		preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
		$fromwhere = $matches[1];
		$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
		$max_page = $max_num_pages = ceil($numposts / $posts_per_page);
	}
	if( !$paged )
	  $paged = 1;
	if($paged == $max_page)
	  return true;
	else
	  return false;	
}

Posted January 24th, 2007 - Permalink
Categories: PHP - Programming - Snippets - Wordpress
No Comments »
 
More WordPress Stuff

So I’ve gotten all the posts over from the old system. I’ve had alot of trobule with getting iG:Syntax Hiliter to behave, though it’s mostly my own fault for not understanding the options and the styling. The “plain text” links still weren’t working, so I took them off.

I’ve been finding little flaws in the theme here and there, and just today created a home.php to override the index and give this nice custom job, more like the old page. I also had problems getting scrobbler to work. I finally realized it wasn’t scrobbler that was messed up, it was wp-cache making the dynamic last.fm content into cached copies.

I figured that out after pulling scrobbler and inserting my own last.fm code straight into the template. Not the best solution, but I never intend on releasing this template as public anyway.

Still chugging away at the static site, should have some of my content back soon.

Posted August 28th, 2006 - Permalink
Categories: PHP - Themes - Wordpress
No Comments »
 
Hello world!

Well, I’ve gotten the new wordpress up, and finished up (I hope) my theme here. Hopefully there aren’t any little glitches in it, I’m bad at testing code out. Now I’ll start porting in all the content from the old site, which I’ve moved to static.velvetcache.org.

Don’t bother going there though, I hardcoded too much of the Smarty engine into it and so everything is broken since it’s moved to a new domain. Yet another thing to grep and fix :) Oh well, I really like wordpress, so it’s worth it.

Also, maybe it’s just me but I’m having issues with loading some pages, I think wp-cache is buggy, something else to figure out. If something doesn’t load, just try a few refreshes, sorry.

Update: Found the offending line of code, wp-cache should work nicely now :)

Posted August 27th, 2006 - Permalink
Categories: CSS - PHP - Themes - Wordpress
No Comments »
 
State of the Website Address

This is a post from a previous system. Information, links and images may not be vaild.

I’ve been wokring on alot of web apps recently, and I’ve been cleaning up the 404’s and misc. pages around velvetcache.

Today I tried (reasonably hard) to get typo running. I never did, probably because there seems to be absolutely no documentation out there and I don’t know ruby. Instead I dropped in a copy of WordPress using Dreamhost’s excellent One-Click install system.

I’ve got to say, WordPress is extremely nifty, and the amount of plugins is enormous. If you want to look at it it’s at http://typo.velvetcache.org/ (dead link), though to be honest it’s just a wordpress blog with some broken css (plugin issues).

I’m going to play with it some more and try to theme it. It’s a really nice app with lots of support, and to be honest, I doubt I’ll ever “finish” my system (I’m still inserting new blog posts via phpMyAdmin!) and even if I do, it won’t be anywhere near as cool. I guess we’ll see.

Posted August 24th, 2006 - Permalink
Categories: PHP - Themes - Wordpress
No Comments »