• The function below is used to count posts per category and return the number of posts.

    function get_post_count($id) {
      //return count of post in category
        $cat = get_category($id);
        $count = (int) $cat->count;
        $taxonomy = 'category';
        $args = array(
          'child_of' => $id,
        );
        $tax_terms = get_terms($taxonomy,$args);
        foreach ($tax_terms as $tax_term) {
            if ($tax_term->parent == $id) {
            $count +=$tax_term->count;
        }
        }
        return $count;
    }

    I then call the function using this line of code:

    .get_post_count(get_cat_ID( $item->title ))

    It appears that every category that includes an ‘&’ or ‘& amp;’ in the database automatically returns 4 counts. For example, the category ‘computers’ returns 1 if and only if there is a post assigned to the category. However, the category ‘Computers & Electronics’ returns 4 regardless if there is a post in the category or not. For some reason adding an ‘&’ to the category name causes the post count to output 4. Any ideas?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Post Count Function Returns Additional Counts for Ampersands’ is closed to new replies.