WordPress category listings

WordPress provides a lot of functionality, some of which you just don’t realize until you need it. However, read the documentation carefully, as the canned methods it provides for handling content and taxonomy may not be what you are looking for.  This is the situation I recently ran into with a category listing page.

This category page was the basic /website/category-name page that you direct users to when you need them to see a listing of all posts within a specific category. For example on this site, you can go to http://www.davehoran.com/category/software/wordpress-software/ to see all the items in the WordPress category. However, when you add post to more than one top-level category, things start to get strange.  Here, I’ll describe the problem, and one possible solution (but not guaranteed to be the only or the best one).

I had a situation where I needed to render a list of items, but I noticed that some were not rendering at all. Some of the posts had been place in multiple categories so they would presumably show up in multiple category listings. Makes sense, but after looking into the custom category.php file that I quickly created for the child theme I was using, I noticed I had used the following:

$categories = get_the_category();

This generated an array, but rather than the one category array element I expected, it creates an element for each category that was used in the results. However, the array didn’t put the “main” category, i.e. the category specified in the URL, as the first element in the array. My code had been picking up the first item in the array and run with it…leaving anything else in the dust. In many cases, the category specific in the URL wasn’t the first item.

To solve this, I broke out the current URL (which contained the category slug) into an array. I then looped through the $categories array I created looking for a match on the slug. Once found, I saved the category ID from this $categories array element, and broke from the loop. I then used this ID in constructing a WP_Query to pull the proper posts.

$categories = get_the_category(); // get categories array for all items in category

// there can be multiple categories per post, so we need to find the main one matching the URL
$main_cat_id = “”;
$url_array = explode(“/”,$_SERVER[‘REQUEST_URI’]); // break current URL into parts so we can attampt to match category slug

$main_cat_id = "";

// break current URL into parts so we can attampt to match category slug
$url_array = explode("/",$_SERVER['REQUEST_URI']); 

foreach($categories as $category){
   if (in_array($category->slug, $url_array)){ // if there is a match, save the ID and move on.
     $main_cat_id = $category->cat_ID;
     break;
   }
}
$cat_query_args = array(
  // Include posts from category with ID $cat_id
  'cat' => $main_cat_id,
  // Include all posts
  'posts_per_page' => -1,
  'nopaging' => true 
);
$cat_query = new WP_Query( $cat_query_args );

The query results in $cat_query are looped through to display the category posts with links to their detail pages.

I hope this helps someone running into the same issue.

Leave a Reply

Your email address will not be published. Required fields are marked *