Support » Fixing WordPress » Category Display: Invalid foreach()

  • I’ve been seeing this warning pop up for many people using WP since WP 1.0 and it just appeared for me again after I upgraded from WP 1.02 -> 1.2. Can we please get an official fix for this problem?
    Yes, I have 10 categories but none are displayed. Yes I can easily create new categories but those don’t show up either.
    Summary of problem:
    This line of code: <?php wp_list_cats(); ?>
    causes this warning to appear:
    Warning: Invalid argument supplied for foreach() in /home/myblog/wp-includes/template-functions-category.php on line 304
    My configuration: MySQL 4.x, PHP 4.36, FreeBSD

Viewing 10 replies - 1 through 10 (of 10 total)
  • If you can, post your template, or e-mail it to me: unoamigo@frankentosh.com
    If I remember right it throws that error because you are calling wp_list_cats from within the loop for displaying the blog contents.
    Most likely this is a template problem.

    That is not the case here. I did a fresh install of WP 1.2 and changed the site url in the database, and I still get this message when using the default index.php and css file from WP 1.2.

    <?php list_cats(0, 'All', 'name', '', '', false); ?>
    That work for you?

    It does not work. However,
    if I use this code:
    <?php list_cats(0, ‘All’, ‘name’, ”, ”, ”, ”, ”, false); ?>
    the categories are displayed BUT I STILL GET THE ERROR MESSAGE.

    I have this error in 2 blogs
    1.2mingus
    1.2delta
    Both blogs worked fine last night, but now they are b0rked…
    The loops are fine, and I have not altered the default cat display php statement.

    Got mine.
    Some php tracking code which I had must be interfering with a wp variable. I deleted the tracking code, and the problem went away.
    Still need a script though 🙁

    Ok. I’m pretty sure I figured out why it’s happening. Here’s my setup. If someone could try this an verify, I’d truly appreciate it.
    1) I have ten categories
    2) I have 20 articles but ALL of them have “Post Status” set to “Private”
    That causes the error message in index.php when categories are listed.
    3) If I set the “Post Status” of just one of the articles to “Publish”, the error DOES NOT appear. The article is displayed and just the category that the article is in is listed.
    So, this appears to be a bug in WordPress where, if all articles have “Post Status” set to “Private”, an attempt to list categories causes that error msg to appear.

    The problem is that the query to pull the categories returns nothing, so the $cat_counts array is empty. Changing lines 304-308 of wp-includes/template-functions-category.php from:

    foreach ($cat_counts as $cat_count) {
    if (1 != intval($hide_empty) || $cat_count > 0) {
    $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
    }
    }

    to this:

    if (count($cat_counts)) {
    foreach ($cat_counts as $cat_count) {
    if (1 != intval($hide_empty) || $cat_count > 0) {
    $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
    }
    }
    }

    should fix this error.

    Thanks. Fixed in CVS.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Category Display: Invalid foreach()’ is closed to new replies.