Solution to the WordPress Plugin WPTouch Returns a 404 Error when the Category is Empty.

wordpress-4There is an excellent plugin for WordPress called WPTouch. It’s purpose is to format web content to display more appropriately on portable devices such as the iPhone, BlackBerry, and other similar interfaces. It is extremely customizable. One of the nagging issues that I had had with this plugin is that it contains it’s own function for displaying categories. The problem with this function is that it will display categories with zero posts.

When selecting one of these categories, it will return a 404 Page Not Found error. To correct this go to the core-functions.php file and add an if statement to the function bnc_get_ordered_cat_list(). The if statement will test if the count is greater than zero. If it is, then display the category.

The original code.

function bnc_get_ordered_cat_list() {
// We created our own function for this as wp_list_categories doesn't make the count linkable

global $table_prefix;
global $wpdb;

$sql = "select * from " . $table_prefix . "term_taxonomy inner join " . $table_prefix . "terms on " . $table_prefix . "term_taxonomy.term_id = " . $table_prefix . "terms.term_id where taxonomy = 'category' order by count desc";
$results = $wpdb->get_results( $sql );
foreach ($results as $result) {
echo "<li><a href=\"" . get_category_link( $result->term_id ) . "\">" . $result->name . " (" . $result->count . ")</a></li>";
}
}

The updated code.

function bnc_get_ordered_cat_list() {
// We created our own function for this as wp_list_categories doesn't make the count linkable

global $table_prefix;
global $wpdb;

$sql = "select * from " . $table_prefix . "term_taxonomy inner join " . $table_prefix . "terms on " . $table_prefix . "term_taxonomy.term_id = " . $table_prefix . "terms.term_id where taxonomy = 'category' order by count desc";
$results = $wpdb->get_results( $sql );
foreach ($results as $result) {
if ($result->count>0) {
echo "<li><a href=\"" . get_category_link( $result->term_id ) . "\">" . $result->name . " (" . $result->count . ")</a></li>";
}
}
}