• I have been trying to exclude “uncategorized” from my list of posts. (who would’t?) And nothing has been working. Also, noticed there are several others that experienced the same problem … with no response from the author. So I dug into the code and got lucky.

    On line 379 of collapscatlist.php, there is a for-loop

    for ($i=0; $i<count($categories); $i++) {
        if ($inExclude=='exclude' && !empty($inExclusionArray)) {
          if (in_array($categories[$i]->slug, $inExclusionArray) OR
              in_array($categories[$i]->term_id, $inExclusionArray)) {
            unset($categories[$i]);
          }
        }
      }

    The problem is that the array actually runs from 1 to count, so the last element is always overlooked.

    Changing the for-loop limits fixes the problem

    for ($i=1; $i<=count($categories); $i++)

    Aside from this one bug, its a terrific plugin … and I never would have found the bug if wasn’t so well written. Thanks!
    https://wordpress.org/plugins/collapsing-categories/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter mirgcire

    (@mirgcire)

    My previous post was erroneous. There was something more fundamentally wrong with the algorithm than I realized. Now it appears to be the for loop that is the cause of the problem.

    Anyway here is an algorithm that works:

    if ($inExclude!='exclude' OR empty($inExclusionArray))
            return $categories;
        // else exclude some categories
        $newcategories = array(); // new array to hold not-excluded categories
        foreach ($categories as $obj) { // check both slug and category id
          if (!in_array($obj->slug, $inExclusionArray) AND
              !in_array($obj->term_id, $inExclusionArray)) {
            array_push($newcategories, $obj); // add not-excluded category
          }
        }
        return $newcategories;

    I think the for-loop was not working because categories is sometimes a sparse array. It might be a bug in php. But, whatever the cause, this solution has stood up to more rigorous testing.

    Works! Thanks so much for the fix!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude Categories not working properly’ is closed to new replies.