Forum Replies Created

Viewing 1 replies (of 1 total)
  • Here’s how I fixed it.

    Previous code:

    <?php
    	$query = "SELECT cat_ID, cat_name, category_nicename, category_description, category_parent, category_count FROM $wpdb->categories ORDER BY cat_name";
    	$categories = $wpdb->get_results($query);
    	foreach ( $categories as $category ) {
    		echo("<div class=\"cat_num\">".intval($category->category_count)."</div><div class=\"cat_name\"><a href=\"".get_category_link($category->cat_ID)."\">".$category->category_nicename."</a></div>");
    	}
    	?>

    New code:

    <?php
    	$query = "SELECT * FROM $wpdb->term_taxonomy JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) WHERE $wpdb->term_taxonomy.taxonomy = 'category' ";
    	$categories = $wpdb->get_results($query);
    	foreach ( $categories as $category ) {
    		echo("<div class=\"cat_num\">".intval($category->count)."</div><div class=\"cat_name\"><a href=\"".get_category_link($category->term_id)."\">".$category->name."</a></div>");
    	}
    	?>

    Basically, instead of grabbing the fields from the categories table, you grab anything that’s a category in the taxonomy table and look up the title in the terms table. I’m sure there’s an even smarter or more proper way of doing it (such as using the relationships lookup table) but for my simple little category link list it did the trick.

Viewing 1 replies (of 1 total)